動態(tài)靜態(tài)
getXXXByXXX 獲取的是動態(tài)的element | elements
碴倾,querySelector | querySelectorAll
獲取的是靜態(tài)的element | elements
靜態(tài)不會跟隨Document
變化
例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<ul>
<li>a</li>
</ul>
<script>
window.onload = function() {
let qLiEle = document.querySelectorAll('li');
let liEle = document.getElementsByTagName("li");
document.getElementsByTagName('ul')[0].appendChild(document.createElement("li"));
console.log(qLiEle.length) // 1
console.log(liEle.length); // 2
}
</script>
</body>
</html>
性能
便捷性
querySelector | querySelectorAll
支持css選擇器,在多級查詢中使用起來更便捷