再談querySelector和querySelectorAll的區別與聯系_javascript技巧
來源:懂視網
責編:小采
時間:2020-11-27 21:02:53
再談querySelector和querySelectorAll的區別與聯系_javascript技巧
再談querySelector和querySelectorAll的區別與聯系_javascript技巧:先按W3C的規范來說這兩個方法應該返回的內容吧: querySelector: return the first matching Element node within the node's subtrees. If there is no such node, the method must return null.(返回指定元素節
導讀再談querySelector和querySelectorAll的區別與聯系_javascript技巧:先按W3C的規范來說這兩個方法應該返回的內容吧: querySelector: return the first matching Element node within the node's subtrees. If there is no such node, the method must return null.(返回指定元素節

先按W3C的規范來說這兩個方法應該返回的內容吧:
querySelector:
return the first matching Element node within the node's subtrees. If there is no such node, the method must return null.(返回指定元素節點的子樹中匹配selector的集合中的第一個,如果沒有匹配,返回null)
querySelectorAll:
return a NodeList containing all of the matching Element nodes within the node's subtrees, in document order. If there are no such nodes, the method must return an empty NodeList. (返回指定元素節點的子樹中匹配selector的節點集合,采用的是深度優先預查找;如果沒有匹配的,這個方法返回空集合)
使用方法:
代碼如下:
var element = baseElement.querySelector(selectors);
var elementList = baseElement.querySelectorAll(selectors);
這在BaseElement 為document的時候,沒有什么問題,各瀏覽器的實現基本一致;但是,當BaseElement 為一個普通的dom Node的時候(支持這兩個方法的dom Node),瀏覽器的實現就有點奇怪了,舉個例子:
代碼如下:
Test
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
再談querySelector和querySelectorAll的區別與聯系_javascript技巧
再談querySelector和querySelectorAll的區別與聯系_javascript技巧:先按W3C的規范來說這兩個方法應該返回的內容吧: querySelector: return the first matching Element node within the node's subtrees. If there is no such node, the method must return null.(返回指定元素節