原文:如何循環遍歷document.querySelectorAll()方法返回的結果

使用JavaScript的forEach方法,我們可以輕松的循環一個數組,但如果你認為document.querySelectorAll 方法返回的應該是個數組,而使用forEach循環它: 執行上面的代碼,你將會得到執行錯誤的異常信息。這是因為,document.querySelectorAll 返回的不是一個數組,而是一個NodeList。 對於一個NodeList,我們可以用下面的技巧來循環 ...

2018-12-18 10:04 0 1819 推薦指數:

查看詳情

document.querySelectorAll遍歷(forEach小解)

document.querySelectorAll兼容性良好,在之前的項目中就其遍歷方式出了錯誤,先做個小結: 1.for循環 傳統遍歷方法 for(var i= 0; i< document.querySelectopAll(".a").length; i ...

Sun Oct 23 02:51:00 CST 2016 0 13514
document.querySelector()和document.querySelectorAll()

HTML5向Web API新引入了 document.querySelector()和document.querySelectorAll()兩個方法,都可以接收三種類型的參數:id(#),class(,),標簽,就像jquery選擇器,參數需要是合法的CSS選擇語法。 用起來更方便的從DOM中選 ...

Fri Mar 31 22:50:00 CST 2017 0 6735
document.querySelector和querySelectorAll方法

querySelector和querySelectorAll是W3C提供的 新的查詢接口,其主要特點如下: 1、querySelector只返回匹配的第一個元素,如果沒有匹配項,返回null。 2、querySelectorAll返回匹配的元素集合,如果沒有匹配項,返回 ...

Tue Aug 02 05:52:00 CST 2016 0 6385
javascript之 原生document.querySelector和querySelectorAll方法

querySelector和querySelectorAll是W3C提供的 新的查詢接口,其主要特點如下: 1、querySelector只返回匹配的第一個元素,如果沒有匹配項,返回null。 2、querySelectorAll返回匹配的元素集合,如果沒有匹配項,返回 ...

Fri Nov 24 00:59:00 CST 2017 0 5857
querySelectorAll()方法

querySelectorAll()方法 調用的對象包括:Document(文檔) DocumentFragment(文檔片段) Element(元素) querySelectorAll()方法接收的參數與querySelector()方法一樣,也是一個css選擇符,但返回的是所有匹配的元素 ...

Sun Aug 19 17:27:00 CST 2018 0 2318
querySelectorAll 和 getElementBy 方法的區別

作者:簡生 鏈接:https://www.zhihu.com/question/24702250/answer/28695133 來源:知乎 1. W3C 標准 querySelectorAll 屬於 W3C 中的 Selectors API 規范 [1]。而 getElementsBy ...

Tue Apr 16 17:57:00 CST 2019 0 488
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM