导航
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>
document.
和 元素.
的方式调用<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);
document.
和 元素.
的方式调用<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>
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>
document.
和 元素.
的方式调用<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>
document.
和 元素.
的方式调用<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>
document
直接访问document.title
获取的是 title 元素中的文本而不是 title 整个元素var body = document.body;
var head = document.head;
document.documentElement
<ul>
<li>
<h2>我是h2</h2>
<a>超链接</a>
<p>p标签</p>
</li>
</ul>
document.parentNode
为 null<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>
和上面 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 方法)