gpt4 book ai didi

javascript - jQuery eq() 选择太多元素

转载 作者:行者123 更新时间:2023-12-03 00:27:46 25 4
gpt4 key购买 nike

我根据用户点击的关闭按钮删除一个div,所以我使用eq(),问题是例如用户点击eq(0),第一个说一些(如预期的那样) ),但第二个div也被认为是eq(0)和一些。就好像我关闭了 div eq (0),其他两个 div 也关闭了,就好像它们是 eq (0),但这一切都只需单击 div eq (0) 例如即可关闭只有一个,我已经尝试过使用 stopImmediatePropagation (),但是当我关闭 div 时,它会关闭以上所有内容,而当我不使用它时,所有内容都会关闭!有谁知道问题出在哪里吗?

下面是 eq() 代码的示例:

$('.' + tabHistoryBtn).click(function() { //close button
var index = $(this).index();
if(index == 0){
$('.' + currentDivTabBarContent).eq(0).remove(); //content to remove
$('.' + tabHistory).eq(0).remove();
if(currentbtn == "home"){
countContentHome = countContentHome -1;
}if(currentbtn == "explore"){
countContentExplore = countContentExplore -1;
}
alert("1");
}
if(index == 1){
$('.' + currentDivTabBarContent).eq(1).remove();
$('.' + tabHistory).eq(1).remove();
if(currentbtn == "home"){
countContentHome = countContentHome -1;
}if(currentbtn == "explore"){
countContentExplore = countContentExplore -1;
}
alert("2");
}
if(index == 2){
$('.' + currentDivTabBarContent).eq(2).remove();
$('.' + tabHistory).eq(2).remove();
if(currentbtn == "home"){
countContentHome = countContentHome -1;
}if(currentbtn == "explore"){
countContentExplore = countContentExplore -1;
}
alert("3");
}
});

这是带有完整代码的 Jsfiddle:http://jsfiddle.net/willianjohns/w5eLs8d4/16/

最佳答案

var index = $(this).index(); 始终返回 0。将其替换为 $('.' + tabHistoryBtn).index(this) 将做你所期望的。

http://api.jquery.com/index有关如何使用 $.index() 的更多信息

关于javascript - jQuery eq() 选择太多元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54012215/

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