导航
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>