document.getElementById & document.querySelector
document.getElementById & document.querySelector
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
-
getElementById 仅支持 id 选择器, 返回 An Element object
-
querySelector 支持各种选择器, 返回匹配的第一个 An HTMLElement object
https://developer.mozilla.org/en-US/docs/Web/API/Element
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement
- HTMLElement 是父类,base 接口
- Element 是子类,实现/继承于 HTMLElement 接口
demo
document.getElementById("demo");
<section id="demo">
</section>
document.querySelector("#demo")
<section id="demo">
</section>
document.querySelectorAll("#demo")
NodeList [section#demo]0: section#demolength: 1__proto__: NodeList
document.getElementById("demo") === document.querySelector("#demo")
true
document.getElementById("demo") == document.querySelector("#demo")
true
typeof document.getElementById("demo")
"object"
typeof document.querySelector("#demo")
"object"
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/12752976.html
未经授权禁止转载,违者必究!