导航


HTML

CSS

JavaScript

浏览器 & 网络

版本管理

框架

构建工具

TypeScript

性能优化

软实力

算法

UI、组件库

Node

冷门技能

获取元素 document(最顶层)

⭐️ getElementById(document)

<div name='box'></div>
<script>
  var box = document.getElementById('box'); // ie8: div ie8+: null
</script>
<div class="container">
  <div id="id-box" class="class-box1"></div>
</div>
<div id="id-box" class="class-box2"></div>
<script>
  const box = document.getElementById('id-box'); // 小写字母
  const box2 = document.getElementById('Id-Box'); // 大写字母
  console.log(
    box, // 匹配第一个:div#id-box.class-box1
    box2 // null
  ); 
</script>

getElementsByTagName(Document、Element)

<div id="container">
  <p>1</p>
  <p>2</p>
</div>
<p>3</p>
<script>
  const oPList = document.getElementsByTagName('p'); 
  const oContainer = document.getElementById('container');
  const oContainerPList = 
        oContainer.getElementsByTagName('P'); // 1. 元素也能调用 2. 大小写也不敏感
  const oSpanList = document.getElementsByTagName('span');
  console.log(
    oPList, // HTMLConllection(3) [p, p, p]
    oContainerPList, // HTMLConllection(2) [p, p]
    oSpanList, // HTMLConllection []
  );
</script>

**通配符 ***

var all = document.getElementsByTagName('*');
console.log(all);

Untitled

⭐️ getElementsByClassName(Document、Element)

<div id="container">
  <p class="desc">1</p>
  <p class="desc">2</p>
</div>
<p class="desc outer">3</p>
<script>
  const oPList = document.getElementsByClassName('desc');
  const oContainer = document.getElementById('container');
  const oContainerPList = 
        oContainer.getElementsByClassName('DESC'); // 1. 元素也能调用 2. 大小写也不敏感
  const oSpanList = document.getElementsByClassName('description');
  console.log(
    oPList, // HTMLCollection(3) [p.desc, p.desc, p.desc.outer]
    oContainerPList, // HTMLCollection(2) [p.desc, p.desc]
    oSpanList // HTMLCollection []
  );
</script>

getElementsByName(Document)(用得少)

<div id="container">
  <p name="desc">1</p>
  <p name="desc">2</p>
</div>
<p name="desc outer">3</p> <!-- name不要写多个名称 -->
<script>
  const oPList = document.getElementsByName('desc');
  const oPList2 = document.getElementsByName('DESC'); // 大小写敏感
  const oSpanList = document.getElementsByName('description');
  console.log(
    oPList, // NodeList(2) [p, p] 最后一个p不会被匹配
    oPList2, // NodeList []
    oSpanList // NodeList []
  );
</script>

⭐️ querySelector、querySelectorAll

Untitled

querySelector(Document、Element、DocumentFragment)

<div id="container">
  <p class="desc">1</p>
  <p class="desc">2</p>
</div>
<p class="desc outer">3</p>
<script>
  const oContainer = document.getElementById('container');
  const oP = document.querySelector('.desc');
  const oP2 = document.querySelector('.DESC'); // 大小写不敏感
  const oSpan = document.querySelector('.description');

  const oContainerP = oContainer.querySelector('.desc');
  console.log(
    oP, // p.desc(container中第一个p)
    oP2,  // p.desc(container中第一个p)
    oSpan, // null
    oContainerP, // p.desc(container中第一个p)
  );
</script>

querySelectorAll(Document、Element、DocumentFragment)

<div id="container">
  <p class="desc">1</p>
  <p class="desc">2</p>
</div>
<p class="desc outer">3</p>
<script>
  const oContainer = document.getElementById('container');
  const oPList = document.querySelectorAll('.desc');
  const oPList2 = document.querySelectorAll('.DESC'); // 大小写不敏感
  const oSpanList = document.querySelectorAll('.description');

  const oContainerPList = oContainer.querySelectorAll('.desc');
  console.log(
    oPList, // NodeList(3) [p.desc, p.desc, p.desc.outer]
    oPList2,  // NodeList(3) [p.desc, p.desc, p.desc.outer]
    oSpanList, // NodeList []
    oContainerPList, // NodeList(2) [p.desc, p.desc]
  );
</script>

body、head

var body = document.body;
var head = document.head;

Untitled

documentElement

document.documentElement

Untitled

⭐️ 父、子、兄弟节点的获取

<ul>
  <li>
  	<h2>我是h2</h2>
		<a>超链接</a>
		<p>p标签</p>
	</li>
</ul>

Untitled

✨ Node.parentNode

<div class="container">
  <p>
    <a href="javascript:;" class="link"></a>
  </p>
</div>
<script>
  const oA = document.getElementsByClassName('link')[0];
  console.log("oA:", oA);
  console.log("oA.parentNode:", oA.parentNode);
  console.log("oA.parentNode.parentNode:", oA.parentNode.parentNode);
  console.log("oA.parentNode.parentNode.parentNode:", oA.parentNode.parentNode.parentNode);
  console.log("oA.parentNode.parentNode.parentNode.parentNode:", oA.parentNode.parentNode.parentNode.parentNode);
  console.log("oA.parentNode.parentNode.parentNode.parentNode.parentNode:", oA.parentNode.parentNode.parentNode.parentNode.parentNode);
  console.log("oA.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode:", oA.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode);
</script>

Untitled

✨ Node.parentElement 获取父元素节点

和上面 parentNode 一样的代码:

<div class="container">
  <p>
    <a href="javascript:;" class="link"></a>
  </p>
</div>
<script>
  const oA = document.getElementsByClassName('link')[0];
  console.log("oA:", oA);
  console.log("oA.parentElement:", oA.parentElement);
  console.log("oA.parentElement.parentElement:", oA.parentElement.parentElement);
  console.log("oA.parentElement.parentElement.parentElement:", oA.parentElement.parentElement.parentElement);
  console.log("oA.parentElement.parentElement.parentElement.parentElement:", oA.parentElement.parentElement.parentElement.parentElement);
  console.log("oA.parentElement.parentElement.parentElement.parentElement.parentElement:", oA.parentElement.parentElement.parentElement.parentElement.parentElement);
  console.log("oA.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement:", oA.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement);
</script>

报错的原因是,#document 不是个元素节点,而是节点。所以倒数第二个 log 返回 null;进而导致最后一个 log 报错(null 上边没有 parentElement 方法)

Untitled