gpt4 book ai didi

javascript - 如果在函数内部使用 this() 选择器,它将引用哪个选择器(jQuery)?

转载 作者:行者123 更新时间:2023-11-29 10:23:12 25 4
gpt4 key购买 nike

例如我有一个函数:

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;
});
}

示例: http://jsfiddle.net/9ubgH/

关于javascript - 如果在函数内部使用 this() 选择器,它将引用哪个选择器(jQuery)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7260126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com