开发者

jQuery toggle not working properly

开发者 https://www.devze.com 2023-02-24 23:59 出处:网络
I have a list of Regions and corresponding countries..when i click on a region, the list should expand while collapsing all other regions. Also when i click on the same region again n again, it should

I have a list of Regions and corresponding countries..when i click on a region, the list should expand while collapsing all other regions. Also when i click on the same region again n again, it should toggle. But in my case every things working fine except that toggle function is not working properly on first click.

$(document).ready(function(){
$("tbody[id*='titl']").live('click',function(){     
    var nextId = ('#' + $(this).next("tbody[id*='tbod']").attr('id'));
    $("tbody[id*='tbod']").each(function(){
    var hideId = ('#' + $(this).attr('id'));    
    if(nextId != hideId)
    {
        $(hideId).hide();       
    }
  开发者_开发百科  else
    {           

        $(hideId).toggle();
    }

});
});


Although you computed nextID with the context tbody[id*='titl'] selector but after this when you call $("tbody[id*='tbod']").each(function() is searching entire DOM for a matching element.

You should write: $(this).next("tbody[id*='tbod']").each(function(){ so that you are searching nearby elements to the live tbody element

0

精彩评论

暂无评论...
验证码 换一张
取 消