1

我一定错过了一些基本的东西,但对于我的生活来说,我无法解决它。

这个想法是当你点击那个点击事件然后解除绑定。还要注意 alert('test') 没有触发。

  $(document).ready(function() {
  $('#menu .cat-item a').each( function() {
    $(this).bind('click',hide).unbind('click',hide);
  });
 });


 function hide(){
   alert('test')
    $("#home-content").fadeIn();
   $("#home-page").fadeOut();
 } 

使用 jQuery 1.4

非常感谢

4

2 回答 2

0

绑定后立即解除绑定!您需要在处理程序 ( hide) 中取消绑定以确保它被执行,否则您可能会考虑这样做:

$(document).ready(function() {
    $('#menu .cat-item a').one("click", hide);
});

one将确保每个元素只执行一次处理程序。

于 2010-05-26T09:48:42.073 回答
0

由于 jQuery 支持流畅的编程风格bind,实际上返回的是this对象。

因此,在这种情况下,您首先添加处理程序,然后立即将其删除。

正确的版本是

$(document).ready(function() {
    $('#menu .cat-item a').each( function() {
         $(this).bind('click',hide);
    });
});


function hide(){
    $(this).unbind('click',hide);
    $("#home-content").fadeIn();
    $("#home-page").fadeOut();
} 
于 2010-05-26T09:49:10.780 回答