再談querySelector和querySelectorAll的區(qū)別與聯(lián)系_javascript技巧
來(lái)源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-27 21:02:53
再談querySelector和querySelectorAll的區(qū)別與聯(lián)系_javascript技巧
再談querySelector和querySelectorAll的區(qū)別與聯(lián)系_javascript技巧:先按W3C的規(guī)范來(lái)說(shuō)這兩個(gè)方法應(yīng)該返回的內(nèi)容吧: querySelector: return the first matching Element node within the node's subtrees. If there is no such node, the method must return null.(返回指定元素節(jié)
導(dǎo)讀再談querySelector和querySelectorAll的區(qū)別與聯(lián)系_javascript技巧:先按W3C的規(guī)范來(lái)說(shuō)這兩個(gè)方法應(yīng)該返回的內(nèi)容吧: querySelector: return the first matching Element node within the node's subtrees. If there is no such node, the method must return null.(返回指定元素節(jié)

先按W3C的規(guī)范來(lái)說(shuō)這兩個(gè)方法應(yīng)該返回的內(nèi)容吧:
querySelector:
return the first matching Element node within the node's subtrees. If there is no such node, the method must return null.(返回指定元素節(jié)點(diǎn)的子樹(shù)中匹配selector的集合中的第一個(gè),如果沒(méi)有匹配,返回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. (返回指定元素節(jié)點(diǎn)的子樹(shù)中匹配selector的節(jié)點(diǎn)集合,采用的是深度優(yōu)先預(yù)查找;如果沒(méi)有匹配的,這個(gè)方法返回空集合)
使用方法:
代碼如下:
var element = baseElement.querySelector(selectors);
var elementList = baseElement.querySelectorAll(selectors);
這在BaseElement 為document的時(shí)候,沒(méi)有什么問(wèn)題,各瀏覽器的實(shí)現(xiàn)基本一致;但是,當(dāng)BaseElement 為一個(gè)普通的dom Node的時(shí)候(支持這兩個(gè)方法的dom Node),瀏覽器的實(shí)現(xiàn)就有點(diǎn)奇怪了,舉個(gè)例子:
代碼如下:
Test
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
再談querySelector和querySelectorAll的區(qū)別與聯(lián)系_javascript技巧
再談querySelector和querySelectorAll的區(qū)別與聯(lián)系_javascript技巧:先按W3C的規(guī)范來(lái)說(shuō)這兩個(gè)方法應(yīng)該返回的內(nèi)容吧: querySelector: return the first matching Element node within the node's subtrees. If there is no such node, the method must return null.(返回指定元素節(jié)