我想使用 Document.querySelector() 方法记录包裹在 <a> 标签中的特定文本
I want to use the Document.querySelector() method to log specific text wrapped in <a> tags
我想记录所有包含在标签中且具有 .link 和 .linkNewLine class 的文本。我想将它们记录在控制台中,以便我可以将它们复制到 excel sheet 中,每个 link 文本在单独的行中。因为我将有一个 excel sheet 和大约 200 links,所以最好通过控制台执行此操作,而不是分别复制粘贴每个值。 Document.querySelector() 可能是这里的正确方法,但我将如何编写查询?
<tr>
<td>
<a class="link linkNewLine" target="_blank" href="/link/foo/bar">The Text I want to grab</a>
</td>
<td>
<a class="link linkNewLine" target="_blank" href="/link/foo/bar">Another line of text I want to grab and put in a new row in excel</a>
</td>
<td>
<a class="linkNewLine" target="_blank" href="/link/foo/bar">This line should not end up in my excel</a>
</td>
</tr>
您可以使用 jquery 在控制台中记录这些文本。这是一个代码片段
$('a').each(function(){
if($(this).hasClass('link') && $(this).hasClass('linkNewLine')){
console.log($(this).text()+'\n');
}
});
正确的方法是querySelectorAll
(querySelector
只有returns一个节点)
(function(n){var a=[];
for(var i=0;i<n.length;++i){
console.log(n.item(i).textContent);
a.push(n.item(i).textContent);
}
document.body.innerHTML=('<pre>'+a.join('\n')+'</pre>')
}(document.querySelectorAll('a.link.linkNewLine')))
<table border="1">
<tr>
<td>
<a class="link linkNewLine" target="_blank" href="/link/foo/bar">The Text I want to grab</a>
</td>
<td>
<a class="link linkNewLine" target="_blank" href="/link/foo/bar">Another line of text I want to grab and put in a new row in excel</a>
</td>
<td>
<a class="linkNewLine" target="_blank" href="/link/foo/bar">This line should not end up in my excel</a>
</td>
</tr>
</table>
我想记录所有包含在标签中且具有 .link 和 .linkNewLine class 的文本。我想将它们记录在控制台中,以便我可以将它们复制到 excel sheet 中,每个 link 文本在单独的行中。因为我将有一个 excel sheet 和大约 200 links,所以最好通过控制台执行此操作,而不是分别复制粘贴每个值。 Document.querySelector() 可能是这里的正确方法,但我将如何编写查询?
<tr>
<td>
<a class="link linkNewLine" target="_blank" href="/link/foo/bar">The Text I want to grab</a>
</td>
<td>
<a class="link linkNewLine" target="_blank" href="/link/foo/bar">Another line of text I want to grab and put in a new row in excel</a>
</td>
<td>
<a class="linkNewLine" target="_blank" href="/link/foo/bar">This line should not end up in my excel</a>
</td>
</tr>
您可以使用 jquery 在控制台中记录这些文本。这是一个代码片段
$('a').each(function(){
if($(this).hasClass('link') && $(this).hasClass('linkNewLine')){
console.log($(this).text()+'\n');
}
});
正确的方法是querySelectorAll
(querySelector
只有returns一个节点)
(function(n){var a=[];
for(var i=0;i<n.length;++i){
console.log(n.item(i).textContent);
a.push(n.item(i).textContent);
}
document.body.innerHTML=('<pre>'+a.join('\n')+'</pre>')
}(document.querySelectorAll('a.link.linkNewLine')))
<table border="1">
<tr>
<td>
<a class="link linkNewLine" target="_blank" href="/link/foo/bar">The Text I want to grab</a>
</td>
<td>
<a class="link linkNewLine" target="_blank" href="/link/foo/bar">Another line of text I want to grab and put in a new row in excel</a>
</td>
<td>
<a class="linkNewLine" target="_blank" href="/link/foo/bar">This line should not end up in my excel</a>
</td>
</tr>
</table>