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

相關文章
相關標籤/搜索