Javascript 如何使用;这";选择符中jQuery.each()的值?
有人能告诉我下面的错误在哪里吗?我想让系统提醒循环中每个列表项的锚文本,但无法思考如何构造“this”语法Javascript 如何使用;这";选择符中jQuery.each()的值?,javascript,jquery,Javascript,Jquery,有人能告诉我下面的错误在哪里吗?我想让系统提醒循环中每个列表项的锚文本,但无法思考如何构造“this”语法 $('.jsGrid ul li').each(function(index) { alert(index + ': ' + $('this .overlayContent a').text()); }); 干杯 Paul此是一个变量,无法在字符串中识别。围绕它构造jQuery对象,并使用find获取要查找的锚元素: alert(index + ': ' + $(this).f
$('.jsGrid ul li').each(function(index) {
alert(index + ': ' + $('this .overlayContent a').text());
});
干杯
Paul
此
是一个变量,无法在字符串中识别。围绕它构造jQuery对象,并使用find
获取要查找的锚元素:
alert(index + ': ' + $(this).find('.overlayContent a').text());
如果搜索$(“this.overlycontent a”)
-jQuery将查找如下构造的元素:
<this>
<div class='overlayContent'><a>Some text here</a></div>
</this>
这里有一些文字
你不能这样使用这个关键字。用这个代替
$('.jsGrid ul li').each(function(index) {
alert(index + ': ' + $('this').find('a').text());
});
或者你可以像这样
$('.jsGrid ul li').each(function(index) {
alert(index + ': ' + $('this').children().text());
});
警报(索引+':'+$('.overlycontent a',this.text());请发布你的html-我试着在这里猜测:我的html与选择器不匹配吗?这不起作用:但这不是你发布的答案?!我是根据你提供的代码回答的,如果你发布一半的代码,你会得到一半的答案。。。下次,请立即发布整个代码,不要在评论中。你在说什么?我不是OP。你的答案与你在小提琴中使用的代码不同。因此,我问为什么不,如果小提琴中的代码不起作用,那么答案中的代码将如何工作?如果我使用该示例,至少在我的html中不起作用,我很感激代码不起作用的任何原因
$('.jsGrid ul li').each(function(index) {
alert(index + ': ' + $('this').find('a').text());
});
$('.jsGrid ul li').each(function(index) {
alert(index + ': ' + $('this').children().text());
});