gpt4 book ai didi

javascript - jquery中的(this)和(_this)有什么区别吗?

转载 作者:行者123 更新时间:2023-12-03 11:02:22 25 4
gpt4 key购买 nike

这两种说法有什么区别

jquery 中的

$(this)$(_this)

最佳答案

$(this) 是上下文的当前元素选择器,但 $(_this) 是普通变量选择器。

例如:

$('p').on('click',function(){
var _this = $('div').eq(0);
console.log($(_this));//first div
console.log($(this));//clicked element 'p'
});

但通常这种类型的变量是这样使用的:

$('p').on('click',function(){
var _this = $(this)//clicked element 'p'
setTimeout(function(){
//$(this) won't refer to clicked element 'p' because it's out of context
//$(_this) will refer to clicked element
});
});

关于javascript - jquery中的(this)和(_this)有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28025497/

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