简体   繁体   中英

jQuery eq() selects too many elements

I'm removing a div according to which close button the user clicks, so I'm using eq (), the problem is that for example the user clicks on eq (0), the first one says some (as expected), but the second div is also considered eq (0) and some. It is as if for example I close the div eq (0) the other two divs also close, as if they were eq (0), but this all with just a click on the div eq (0) for example that was to close only one, I've already tried using stopImmediatePropagation (), but when I close a div it closes all of the above, and when I do not use it all shuts down! Anyone know what the problem is?

Here is an example of the eq () code:

$('.' + 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");
            }
});

Here is the Jsfiddle with the complete code: http://jsfiddle.net/willianjohns/w5eLs8d4/16/

var index = $(this).index(); always returns 0. Replacing that with $('.' + tabHistoryBtn).index(this) will do what you're expecting.

http://api.jquery.com/index has more information on how to use $.index()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM