例如我有一个函数:
function somefunction () {
$('someselector').fadeOut('slow', function() { $(this).remove; });
}
然后这个函数在内部被调用:
$('someselector1').click(function() {
somefunction ();
});
somefunction()
中的 $(this)
是指 someselector()
还是 someselector1()
?据我了解,this()
指的是触发事件的选择器,在本例中为 someselector1。是否正确?
最佳答案
它将引用匹配 someselector
的 DOM 元素. this
设置为元素 fadeOut
正在应用函数。
this
someFunction
内部(但不在 fadeOut
的回调函数内)将是对 window
的引用对象。
function somefunction () {
// "this", unless specifically set, will refer to "window"
$('someselector').fadeOut('slow', function() {
// "this" refers to the element that just finished fading out.
$(this).remove;
});
}
关于javascript - 如果在函数内部使用 this() 选择器,它将引用哪个选择器(jQuery)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7260126/