首页 > 怎么让js只弹出一次

怎么让js只弹出一次

      //选择内容
        $(".joy-alert-list li").click(function(){
            if($(this).hasClass('current')){
                $(this).removeClass('current');
            }else{
            $(this).addClass('current');
            };
            
        //点击确定按钮
        $(".joy-btn").click(function(){
            var currList = $('.current').length;
            alert(currList);
        });    
        });

点击确定按钮,弹出次数一直在增加。第一次选择,弹出一次,然后再选择,点击确定会弹出2次。


$('.joy-alert-list li').click(function(){
  $(this).toggleClass('current');
});
$('.joy-btn').click(function(){
  alert($('.current').length);
});

.joy-btn的点击事件不应该放在.joy-alert-list li的点击事件里面,否则每点击一次li,就增加一次joy-btn的绑定事件,代码应该改为:

         //选择内容
        $(".joy-alert-list li").click(function(){
            if($(this).hasClass('current')){
                $(this).removeClass('current');
            }else{
            $(this).addClass('current');
            };              
        });
        
        //点击确定按钮
        $(".joy-btn").click(function(){
            var currList = $('.current').length;
            alert(currList);
        });         

jquery有个one方法,绑定one的只会执行一次


这源码排版......

【热门文章】
【热门文章】