gpt4 book ai didi

javascript - 如何在另一个函数中使用 $(this) 引用?

转载 作者:行者123 更新时间:2023-11-28 16:12:05 25 4
gpt4 key购买 nike

我有一个这样的案例 -

$('<first-selector>').each(function(){
$(this).click(function(){
$('<second-selector>').click(function(){
/* Here I want to use the top $(this) reference to do some business.
I don't want to use like var thisObj = $(this) and refer it inside
the click function*/
});
});
});

如何在另一个对象中使用$(this)引用?

最佳答案

使用$.proxy

$('div').each(function(){
$(this).click(function(){
$('p').click($.proxy(function(){
console.log($(this)); // returns <div>
/* Here I want to use the top $(this) reference to do some business.
I don't want to use like var thisObj = $(this) and refer it inside
the click function*/
}, this));
});
});

演示:http://jsfiddle.net/TRw4X/

关于javascript - 如何在另一个函数中使用 $(this) 引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12583105/

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