2015-05-27 32 views
4

选择锚文本对所有的锚元素现在创建一个字符串数组我需要创建锚文本的数组象下面这样:通过DOM

$(currentNode).find('input:checkbox').next('a') 

从上面的代码行,我会得到一个对象数组的锚元素,但是我想要一个锚元素中的文本数组。

我的锚标记如下:

<a class="rightCaret" href="#subMenuSelect"> SelectMe </a> 

,我需要创建一个包含它的文本数组 - 即SelectMe

任何帮助,将不胜感激

+0

您可以用'.MAP()',并返回'文本()'或'HTML()' – qtgye

回答

3

您可以使用map()建立填入所需的属性的数组。试试这个:

var aTexts = $(currentNode).find('input:checkbox').next('a').map(function() { 
    return $(this).text(); 
}).get(); 

console.log(aTexts); 
0

使用jQuery each()可以做

var anchorTextA = []; 
$(currentNode).find('input:checkbox').next('a').each(function(){ 
    anchorTextA.push($(this).text()); 
}); 
1

使用.map()功能:

$(currentNode).find('input:checkbox').next('a').map(function(){ 
    return $(this).text() 
}).get(); 
1

使用.map()

var array = $(currentNode).find('input:checkbox').next('a').map(function() { 
    return $(this).text(); 
}).get()