簡體   English   中英

從標簽中獲取選擇器值

[英]Grabbing selector value from a tag

簡而言之:

這段代碼:

for(let i = 0; i <= items.length; i++){
    console.log(items[i])
}

給我這個數據:

<a class="photo ajax2" target="_blank" href="/profile/show/3209135.html" data-first="1" data-next="3206884">
<a class="photo ajax2" target="_blank" href="/profile/show/3206884.html"  data-next="3209135">
<a class="photo ajax2" target="_blank" href="/profile/show/3209135.html" data-next="3209755">

我想從所有鏈接中獲取data-next值。 我怎樣才能做到這一點? 我試過這個:

for(let i = 0; i <= items.length; i++){
    console.log(items[i].querySelector('a["data-next"]'));
}

但這不起作用。 我只需要香草JS。

已回答

items[i]指的是元素本身,因此querySelector()不會返回任何內容。 嘗試使用getAttribute()

console.log(items[i].getAttribute("data-next"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM