任何人都可以請建議我去哪裏錯了以下?我希望系統在循環中提醒每個列表項的錨文本,但不能考慮如何構造'this'語法?如何在選擇器中使用「this」的jQuery.each()?
$('.jsGrid ul li').each(function(index) {
alert(index + ': ' + $('this .overlayContent a').text());
});
乾杯 保羅
任何人都可以請建議我去哪裏錯了以下?我希望系統在循環中提醒每個列表項的錨文本,但不能考慮如何構造'this'語法?如何在選擇器中使用「this」的jQuery.each()?
$('.jsGrid ul li').each(function(index) {
alert(index + ': ' + $('this .overlayContent a').text());
});
乾杯 保羅
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>
$('.jsGrid ul li').each(function(index) {
alert(index + ': ' + $(this).find('.overlayContent a').text());
});
你不能使用這樣的關鍵字。改用此
$('.jsGrid ul li').each(function(index) {
alert(index + ': ' + $('this').find('a').text());
});
,或者你CANDO這樣的
$('.jsGrid ul li').each(function(index) {
alert(index + ': ' + $('this').children().text());
});
警報(指數+ ':' + $( 'overlayContent一',這一點)的.text()); – mpm 2012-04-05 11:20:03
請張貼您的HTML - 我試圖在這裏猜測它:http://jsfiddle.net/mplungjan/Jf7ub/ – mplungjan 2012-04-05 11:35:09