js基础总结01 --操做DOM

一、选择对象

  • 经过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()等方法

二、操做属性

  • 直接经过修改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>
示例

三、绑定事件

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

四、获取属性和值

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

五、操做节点

 

 以上要注意是何时是 HTMLCollection,何时是NodeList,二者的具体区别,待后续补充吧。css

相关文章
相关标签/搜索