简而言之:
此代码:
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"));