js基础总结01 --操作DOM

时间:2019-11-07
本文章向大家介绍js基础总结01 --操作DOM,主要包括js基础总结01 --操作DOM使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、选择对象

  • 通过id来选择绑定一个dom节点 :document.getElementById('p1');
  • 通过类名来绑定一个类数组的对象集合,:document.getElementsByClassName('p');//类似的还有 document.getElementsByName、document.getElementsByName等
  • 通过css选择器来返回第一个匹配的dom节点:document.querySelector('#p1');
  • 通过css选择器来返回一个类数组的对象集合:document.querySelectorAll('p');
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>test</title>
</head>
<body>
  <p id="p1">1</p>
  <p class="p">2</p>
  <p class="p">3</p>
<script>
  window.onload = function(){
    document.getElementsByName
    document.getElementsByTagName
    console.log(document.getElementById('p1'));//<p id="p1">1</p>
    console.log(document.getElementById('p2'));//null,找不到
    console.log(document.getElementsByClassName('p'))//HTMLCollection(2) [p.p, p.p],返回类数组的对象集合
    console.log(document.querySelector('#p1'));//<p id="p1">1</p>
    console.log(document.querySelector('#p2'));//null,找不到返回null
    console.log(document.querySelector('p'));//<p id="p1">1</p>,返回匹配到的第一个节点
    console.log(document.querySelectorAll('p'));//NodeList(3) [p#p1, p.p2, p],返回匹配到的类数组的对象集合
    console.log(document.querySelectorAll('p').pop());//test.html:24 Uncaught TypeError: document.querySelectorAll(...).pop is not a function
  }
</script>
</body>
</html>
示例
注意:document.getElementsByClassName('p')等返回的是一个HTMLCollection 对象集合,document.querySelectorAll('p')返回的是一个NodeList 对象集合,两者没有很大的不同,但要注意‘访问 HTMLCollection 项目,可以通过它们的名称、id 或索引号访问 NodeList 项目,只能通过它们的索引号’--这是w3School里的解释。
注意:类数组的对象集合之所以不能称为数组,是因为它们没有数组的pop(),join()等方法

2、操作属性

  • 直接通过修改style来修改属性:document.getElementById('p').style.color = 'red';
  • 通过添加类名来实现:document.getElementById('p').className = 'red';//会覆盖原有的类
  • 通过节点的classList添加一个类:document.getElementById('p').classList.add('red');//不会覆盖原有的,从尾部添加
  • 通过setAttribute来直接向html标签中添加class="xxx"属性:document.getElementById('p').setAttribute('class','red');
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>test</title>
  <style>
    .red{
      color:red;
    }
  </style>
</head>
<body>
  <p id="p" class="p-default">1</p>
  <p>2</p>
<script>
  window.onload = function(){
    // document.getElementById('p').style.color = 'red';
    // document.getElementById('p').className = 'red';//直接给选定节点设置类名,会覆盖掉原有的类
    // document.getElementById('p').classList.add('red');//直接给节点添加一个类,排在原有类的后面,添加已有的类名。及不操作
    // document.getElementById('p').classList.remove('p-default');//删去选定的类
    // document.getElementById('p').setAttribute('class','red');//直接向html标签里添加class="xxx"的属性
  }
</script>
</body>
</html>
示例

3、绑定事件

  • 直接才html标签中绑定事件,通过<p onclick="functionName()"></p>,类似的形式进行绑定
  • 对绑定好的dom对象, document.getElementById('p').onclick = function(){}类似的方法进行绑定
  • 添加事件监听的方式 document.getElementById('p').addEventListener('click',function(){})进行绑定

4、获取属性和值

  • 获取html内容:document.getElementById('p').innerHTML
  • 直接通过属性名获取:document.getElementById('p').属性名
  • 通过getAttribute获取:document.getElementById('p').getAttribute('属性名')

5、操作节点

 以上要注意是什么时候是 HTMLCollection,什么时候是NodeList,两者的具体区别,待后续补充吧。

原文地址:https://www.cnblogs.com/Zxq-zn/p/11810165.html