JavaScript Dom

1.dom樹

.png
dom樹.png

瀏覽器在解析我們的網(wǎng)頁結(jié)構(gòu)的時候,會產(chǎn)生一個document
可以通過JavaScript操控這些元素

2.

document.getElementById('h1title').textContent=12345
var el =document.getElementById('h1title')
el.textContent=12345

通常我們會采取第二種,將el賦值,因為這樣可以更好的瀏覽代碼

3. document.querySelector

querySelector 不僅可以選擇id,還可以選擇class元素,就想css選擇器一樣

var el =document.querySelector('#h1title')
el.textContent='78979'
  <h1 id="h1title">
      <em>@@</em>
  </h1>
 

 <script>
var el =document.querySelector('#h1title em')
el.textContent='123'
</script>

下面這行代碼就是修改<h1>下面的<em>標(biāo)簽里面的內(nèi)容

4.document.querySelectorAll

document.querySelectorAll 返回來是一個 數(shù)組

<h2 class="h2title">hei</h2>
  <h2 class="h2title">hei</h2>
  <h2 class="h2title">hei</h2>
  <h2 class="h2title">hei</h2>
  <h2 class="h2title">hei</h2>
  <h2 class="h2title">hei</h2>
  <h2 class="h2title">hei</h2>
  <h2 class="h2title">hei</h2>

  <script>
var el =document.querySelectorAll('.h2title')
console.log(el)  //會發(fā)現(xiàn)這是一個數(shù)組
console.log(el.length)

var arraylength=el.length
//使用數(shù)組遍歷,改變值
for(var i=0;i<arraylength;i++){
  el[i].textContent='heihei'
}
  </script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容