2012-04-05 24 views
1

任何人都可以請建議我去哪裏錯了以下?我希望系統在循環中提醒每個列表項的錨文本,但不能考慮如何構造'this'語法?如何在選擇器中使用「this」的jQuery.each()?

$('.jsGrid ul li').each(function(index) { 
    alert(index + ': ' + $('this .overlayContent a').text()); 

}); 

乾杯 保羅

+0

警報(指數+ ':' + $( 'overlayContent一',這一點)的.text()); – mpm 2012-04-05 11:20:03

+0

請張貼您的HTML - 我試圖在這裏猜測它:http://jsfiddle.net/mplungjan/Jf7ub/ – mplungjan 2012-04-05 11:35:09

回答

1
alert(index + ': ' + $('.overlayContent a',this).text()); 
+0

我的HTML與選擇器不匹配嗎?這是行不通的:http://jsfiddle.net/mplungjan/Jf7ub/ – mplungjan 2012-04-05 11:29:53

+0

試試這個:http://jsfiddle.net/camus/tA3Dn/ – mpm 2012-04-05 15:32:50

+0

但是這不是你發佈的答案?! – mplungjan 2012-04-05 17:02:56

1

this是一個變量,也不會得到認可字符串裏。構建jQuery對象繞過它,使用find得到你正在尋找的錨元素:如果你搜索$("this .overlayContent a")

alert(index + ': ' + $(this).find('.overlayContent a').text()); 

- jQuery將尋找這樣的構成要素:

<this> 
    <div class='overlayContent'><a>Some text here</a></div> 
</this> 
+0

爲什麼這不起作用?錯誤的HTML? http://jsfiddle.net/mplungjan/Jf7ub/ – mplungjan 2012-04-05 11:28:05

+0

你是什麼意思「工作?」它應該做什麼? – Dennis 2012-04-05 23:18:04

0
$('.jsGrid ul li').each(function(index) { 
    alert(index + ': ' + $(this).find('.overlayContent a').text()); 
}); 
0

你不能使用這樣的關鍵字。改用此

$('.jsGrid ul li').each(function(index) { 
    alert(index + ': ' + $('this').find('a').text()); 
}); 

,或者你CANDO這樣的

$('.jsGrid ul li').each(function(index) { 
    alert(index + ': ' + $('this').children().text()); 
}); 
相關問題