开发者

One click event to do different things

I only want:

$('.div_rightColumn').fadeOut("fast");
$('.div_rightColumn').fadeIn("fast");

to run on the first click. Is there a way to tell jQuery to only do that the first time it is clicked and only run:

$('.name_filter img').live("click", function() {
      $('#contactMessage').fadeOut();
      $('.div_rightColumn').css({"margin" : "3px 0 0 0"});
});

the rest of the clicks? Below is full...

$('.name_filter img').live("click", function() {
      $('#contactMessage').fadeOut(开发者_StackOverflow中文版);
      $('.div_rightColumn').fadeOut("fast");
      $('.div_rightColumn').fadeIn("fast");
      $('.div_rightColumn').css({"margin" : "3px 0 0 0"});
});


You could do something like:

var run = 0;
$('.name_filter img').live("click", function() {
      $('#contactMessage').fadeOut();
      if(run === 0){
           $('.div_rightColumn').fadeOut("fast").fadeIn("fast");
           run++;
       }
      $('.div_rightColumn').css({"margin" : "3px 0 0 0"});
});

Also, as I said in the comment above you should really, really change your live to a delegate like:

$('.name_filter').delegate("img", "click", function() {
     //code
});


If you want to listen for an event only once, you can use .one

http://api.jquery.com/one/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜