我想先查找html文档中所有具有相同类名称并且是锚定标记并从中获取文本的元素。对于数组中元素的JQuery .text()
HTML:
<div class="links" id="one">
<span class="info">useless links</span>
<a class="anchorTag" href="www.sample.com">Click me!</a>
<a class="anchorTag" href="www.sample.com">Click me again!</a>
<a class="anchorTag" href="www.sample.com">Click me once again!</a>
<a class="anchorTag" href="www.sample.com">Click me one last time!</a>
</div>
<div class="links" id="two">
<span class="info">secret links</span>
<a class="anchorTag" href="www.sample.com">Click me!</a>
<a class="anchorTag" href="www.sample.com">Click me again!</a>
<a class="anchorTag" href="www.sample.com">Click me once again!</a>
<a class="anchorTag" href="www.sample.com">Click me one last time!</a>
</div>
代码:
$('.links').each(function(){
if($(this).find("span.info").text() == "secret link"){
var allLinks = $(this).find("a.anchorTag");
console.log(allLinks[0].text());
}
});
错误:
console.log(allLinks[0].text()) is not a function
如果我打印变种的长度allLinks,它返回正确的尺寸,但它在访问i时不会打印数组中特定元素的文本吨与索引?为什么?
ahh我明白了......我不知道这是一个集合,但这澄清了我对这个主题的整个问题。所以它也可能只是将该集合转换为数组,但我想只是直接解决anchorTag并解析文本 – Bajro
是的,如果您希望可以使用jquery的.toArray()将其更改为真数组,方法,但是你不能再对它们执行jquery操作,除非你在jquery中重新包装它们。 – Cameron637