1
$('nav .show_all').on('click', function() {
    $('#all_pages').
    addClass('active').
    css('opacity', 0).
    animate({
        opacity: 1
    }, 1000);
    Book.all_pages();
    return false;
});



 all_pages: function() { 
 alert ('test1'); // test here is ok
 $('#slider li').click(function() {
 alert ('test2'); // this test is not ok
 });
 }

以上是用户点击 nav.showall 时的示例,它打开了一个 div #allpage。这里的错误是当用户关闭 div 并再次打开它时,函数运行的次数受 div 关闭次数的影响。

例如,当用户第一次打开 div 框时,它会提示一次 'test' ,如果用户打开 div 框,关闭它,然后再次打开,它会提示两次 'test' 等等. 如何解决这个问题?

4

1 回答 1

2

在 Javascript 中分配一个全局变量并在单击对象时增加它的计数器,以便您可以根据要求进行跟踪这里是虚拟数据:

var i=0;
function your_function(){
     ///.......
     // your code goes here
     if (i % 2 == 0) {
         alert('You Open it :)');
     }
     i++;
}
于 2013-01-02T05:56:08.163 回答