[英]Why does jQuery select only one element when chaining .attr() in selector?
[英]Why does jQuery class selector only return one element?
該文檔中的類有多個div。
從控制台中可以看到, document.getElementsByClassName
產生:
document.getElementsByClassName('current-pad-o')
HTMLCollection (2) = $8
0 <div class="red current-pad-o"> first value </div>
1 <div class="red current-pad-o"> second value </div>
和具有相同類選擇器的jquery產生:
$('.current-pad-o')
// (the first div only - no collection)
<div class="red current-pad-o"> first value </div>
我希望也從jquery語句的集合。 這些都是在Safari和Firefox中輸出的。
根據jQuery Class Selector文檔 ,第二個選擇器選擇具有給定類的所有元素 。
為什么jquery只返回一個而不是一個集合?
您沒有jQuery,正在使用調試器的document.querySelector()
快捷方式。 如果使用$$('.current-pad-o')
,則可以全部使用。
要驗證您沒有使用jQuery,請在命令行中輸入以下內容:
console.log($)
對於querySelector,您將看到以下內容:
function $(selector, [startNode]) { [Command Line API] }
對於jQuery,您將看到以下內容:
function (a,b){return new n.fn.init(a,b)}
參考: 控制台表達式
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.