2

ユーザーがリンクをクリックすると起動する関数があります。彼らが別のリンクをクリックしたとき、私は彼らがクリックした最初のリンクを変更したいと思います。どうすればこれを行うことができますか?これは私がこれまでに持っているものです:

$('a').click(function(){
  //some code ...
  if ( $(this).attr('id') != last.attr('id') ) {
    $('a').click(function()
      /*the original $this*/.parent().parent().quickFlipper({refresh: 0});
      /*the original last*/.parent().parent().quickFlipper({refresh: 0});
    });
    var that = this;
    var that2 = last;
    setTimeout(function(){
      $(that).parent().parent().quickFlipper({refresh :0});
      $(that2).parent().parent().quickFlipper({refresh :0});
    }, 1500);
  //some more code ...
});

よろしくお願いします。私が何をしようとしているのかわからない場合は、質問してください。

4

4 に答える 4

3

これは、クロージャーを使用する良い機会です。

$('a').click((function () {

   //private
   var lastClicked;

   return function () {
      //your code

      if(lastClicked) {
         //do something to the last clicked element
      }

      lastClicked = this;
   };
})());

フィドル: http: //jsfiddle.net/iambriansreed/Mus6N/

于 2012-07-23T17:07:30.310 に答える
0
var previousElement;

$('a').click(function(){
  //some code ...
  previouseElement = ($this);
  if ( $(this).attr('id') != last.attr('id') ) {
    $('a').click(function()
      $(this).parent().parent().quickFlipper({refresh: 0});
      previousElement.parent().parent().quickFlipper({refresh: 0});
    });
    var that = this;
    var that2 = last;
    setTimeout(function(){
      $(that).parent().parent().quickFlipper({refresh :0});
      $(that2).parent().parent().quickFlipper({refresh :0});
    }, 1500);
  //some more code ...
});
于 2012-07-23T17:04:26.023 に答える
0
var lastClicked = null;

$('a').click(function(){
  //some code ...
  if (lastClicked && $(this).attr('id') != lastClicked.attr('id') ) {
       $(this).parent().parent().quickFlipper({refresh: 0});
      lastClicked.parent().parent().quickFlipper({refresh: 0});

    lastClicked = $(this);
  }
});

そのように?

于 2012-07-23T17:04:38.950 に答える
0

最後にグローバル変数を使用します。

何かのようなもの

$(function(){
    var lastClick;
    $('a').click(function(){
        //do stuff to last click here
        lastClick = $(this); //make this at the bottom
    });
});
于 2012-07-23T17:05:36.677 に答える