function() 和 $(this) 中的each()
假设这是我们的函数:
$('ul li').bind('click', function(){
$('iframe').each(function(){
// who is this???
alert(this);
});
});
正如你所看到的,有一个 .each() 语句在里面使用 $(this)
,
$(this)< 将引用什么元素/代码> ???
ul li
?或iframe
?正如你可以猜到的那样,我正在尝试选择每个 iframe(在网页中,与 ul li 无关)
我问这个是因为我通过更大的函数得到了意想不到的结果,
Lets say this is our function:
$('ul li').bind('click', function(){
$('iframe').each(function(){
// who is this???
alert(this);
});
});
As you can see, there is an .each() sentence that is using $(this)
inside,
What element will be referenced by $(this)
??? ul li
? or iframe
? I'm trying, as you can guess, to select each iframe (in the webpage, nothin to do with ul li, there)
I ask this because i am getting unexpected results with a way larger function,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
this
指的是最具体函数作用域的对象。如果您想从
iframe
内部函数内部引用this
的ul li
版本,则必须在它周围形成一个闭包,像下面这样:this
refers to the object of the most specific function scope.If you want to reference the the
ul li
version ofthis
from inside theiframe
inner function, you would have to form a closure around it, like the following:http://api.jquery.com/each/
HTML:
Javascript:
警报输出:
0: foo
1: bar
this
将引用iframe
对象。上面的代码取自 jQuery 文档。
您可能想告诉我们预期的结果是什么,也许还可以在此处发布整个代码。
http://api.jquery.com/each/
HTML:
Javascript:
Alert output:
0: foo
1: bar
this
will refer to theiframe
objects.The above code is taken from the jQuery documentation.
You might want to tell us what the expected results, and maybe post the whole code here.
它指的是 iframe 你可以从这里检查它 http://jsfiddle.net/x2XyU/1/
It refers to iframe u can check it from here http://jsfiddle.net/x2XyU/1/