动态静态
getXXXByXXX 获取的是动态的element | elements
,querySelector | querySelectorAll
获取的是静态的element | elements
静态不会跟随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选择器,在多级查询中使用起来更便捷
网友评论