简体   繁体   English

我想使用Document.querySelector()方法记录包装在<a>标签中的</a>特定文本

[英]I want to use the Document.querySelector() method to log specific text wrapped in <a> tags

I want to log all the text that is wrapped in tags and that has the .link and the .linkNewLine class. 我想记录包装在标记中的所有文本,这些文本具有.link和.linkNewLine类。 I want to log those in the console so i can copy them into an excel sheet with each link text in a separate line. 我想在控制台中记录这些内容,以便将它们复制到一个Excel工作表中,每个链接文本都放在单独的行中。 As I will have an excel sheet with about 200 links it would be nice to do this via the the console and not by copy pasting each value separately. 由于我将拥有一个包含约200个链接的excel工作表,因此最好通过控制台而不是通过复制分别粘贴每个值来做到这一点。 Probably Document.querySelector() is the right method here but how would I write my Query? 大概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>

You can use jquery to log those text in console. 您可以使用jquery将这些文本记录在控制台中。 Here is a code snippet 这是一个代码片段

$('a').each(function(){
    if($(this).hasClass('link') && $(this).hasClass('linkNewLine')){
        console.log($(this).text()+'\n');
    }
});

The correct method is querySelectorAll 正确的方法是querySelectorAll

( querySelector only returns a single node) querySelector仅返回单个节点)

 (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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM