gpt4 book ai didi

jquery - 让 .this 进入下一个函数

转载 作者:行者123 更新时间:2023-12-01 06:30:06 27 4
gpt4 key购买 nike

我正在使用 Jquery 属性名称过滤来选择其中包含单词“world_map”的所有元素。我的 html 元素如下所示:#world_map_1#world_map_2 等...

然后,我使用 this.id 在该函数中插入名称,以调用以前缀 world_map_1 开头的不同元素。例如world_map_1_panel_container

一切正常。

但现在我正在寻找一种方法来从第二个函数中的第一个函数获取相同的前缀信息。在同一函数内。如果我再次使用 .that 它将引用第二个函数的选择器,而不是第一个函数的选择器......

这是一些代码:

点击元素#world_map_8将为您提供选择器#world_map_8_panel_container

$('#'+'[id*="world_map"]').click(function() {
$('#' +this.id+ '_panel_container').css('visibility','visible');

// Within this function I want to create a new function and be able to use that
//same selector prefix so I don't have to hard code everything like I have to do
//now. This is the second function within the previous one that's hard coded
//at the moment:

$('#text_button').click(function() {
$('#world_map_8_panel_movie_container').css('display','none');
});
});

感谢您的任何提示!

最佳答案

您可以对此进行重新分配:

var that = this;

并在里面使用that

$('#text_button').click(function() {
var something = that.id

这是“先前的原始”外部this

所以:

$('#'+'[id*="world_map"]').click(function() {
var that = this;
$('#' +that.id+ '_panel_container').css('visibility','visible');
$('#text_button').click(function() {
$('#' +that.id+ '_panel_container').css('display','none');

});
});

that 在其声明的完整范围内仍然可用。

关于jquery - 让 .this 进入下一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7298176/

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