2016-07-26 50 views
0

我想先查找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时不会打印数组中特定元素的文本吨与索引?为什么?

回答

2

allLinks不是实际的阵列,而是一组DOM元素。也就是说,普通的数组操作符将无法工作。如果你要打印的第一元素的文本中设定的,而不是这样的:

console.log(allLinks[0].text()); 

你应该使用jQuery的。首先()方法是这样的:

console.log(allLinks.first().text()); 
+0

ahh我明白了......我不知道这是一个集合,但这澄清了我对这个主题的整个问题。所以它也可能只是将该集合转换为数组,但我想只是直接解决anchorTag并解析文本 – Bajro

+1

是的,如果您希望可以使用jquery的.toArray()将其更改为真数组,方法,但是你不能再对它们执行jquery操作,除非你在jquery中重新包装它们。 – Cameron637

1

通过索引访问jQuery结果将返回实际的元素本身。

所以错误信息是说在<a>元素上没有text()函数(这是真的)。

所以取而代之,包裹元件备份jQuery中:

var el = allLinks[0]; 
console.log($(el).text()); 
1

你应该尝试遍历所有你想要的类的锚,而不是遍历整个列表并寻找孩子。

$('a.anchorTag').each(function(){ 
    if($(this).text() == "secret link"){ 
     console.log($(this).text()); 
    } 
}); 

的jsfiddle例如:https://jsfiddle.net/a51q9ry3/

+0

好吧...所以不是做一个for循环,而是尝试与上面发布的循环相同,最好对所需的CSS选择器使用“.each()”函数? – Bajro

+1

我相信它在语法上更易于阅读,并且更准确地表示您试图表示的逻辑。这是迭代遍历具有所述类的锚的集合的一种方式。你的问题是“我想找到与类有关的元素”,我从字面上理解=) '$('a.anchorTag')'将返回一个数组,其中包含所有具有类“anchorTag”的锚元素, – Hodrobond

0

你可以在整个文档直接使用每个循环。

<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> 
<div id="output"> 
</div> 

var out = $("#output"); 
$('a.anchorTag').each(function(){ 
    var elem = $(this); 
    out[0].innerHTML += elem.text(); 
    console.log(elem.text()); 
}); 

这里的小提琴 JSFiddle

当您正在搜索的整个HTML身体,为什么如果情况?