2013-12-21 53 views
0

我試圖從一個元素的同胞的子鏈接中獲取文本。jquery sibiling子鏈接文本

<p> 
    <a>This text here</a> 
</p> 
<form></form> 
<ul></ul> 

這是我已經試過:

$('ul').each(function(){ 
    var theText = $(this).siblings('p a').text(); 
    console.log(theText); 
}) 

我試圖做到這一點,而無需使用上一個或下一個。

+0

改變'$(本).siblings( 'P A')。文本() ;'to'$(this).siblings('p')。children('a').text();'[FIDDLE](http://jsfiddle.net/PY2T6/)example –

回答

3

試試這個

$('ul').each(function(){ 
    var theText = $(this).siblings().find('a').text(); 
    console.log(theText); 
}) 

OR

@Satpal評論

$('ul').each(function(){ 
     var theText = $(this).siblings('p').text(); 
     console.log(theText); 
    }) 

DEM0

+2

just a建議'兄弟姐妹('p')'會更好 – Satpal

+0

謝謝你的作品。我結合了Satpal的答案。乾杯! –