JavaScript Dom

1.dom树

.png
dom树.png

浏览器在解析我们的网页结构的时候,会产生一个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>标签里面的内容

4.document.querySelectorAll

document.querySelectorAll 返回来是一个 数组

<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)  //会发现这是一个数组
console.log(el.length)

var arraylength=el.length
//使用数组遍历,改变值
for(var i=0;i<arraylength;i++){
  el[i].textContent='heihei'
}
  </script>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容