admin管理员组

文章数量:1307838

How i can determine the last clicked id ?! Because if i use :

 if(clicks > 1){
     var lc = event.target.id;  // Last Clicked event

     }

It will return the current button clicked id I have something like this :

$('#menu).click(function(event) {

  clicks++;
   if(clicks > 1){
     var lc = event.target.id;  // Last Clicked event

     }
   console.log(lc);

And i have 2 buttons Now if i click on first button console log will show undefinded but if i click on second button it will show his id.I want to prevent that

How i can determine the last clicked id ?! Because if i use :

 if(clicks > 1){
     var lc = event.target.id;  // Last Clicked event

     }

It will return the current button clicked id I have something like this :

$('#menu).click(function(event) {

  clicks++;
   if(clicks > 1){
     var lc = event.target.id;  // Last Clicked event

     }
   console.log(lc);

And i have 2 buttons Now if i click on first button console log will show undefinded but if i click on second button it will show his id.I want to prevent that

Share Improve this question edited May 12, 2012 at 10:52 Xtal asked May 12, 2012 at 10:44 XtalXtal 3051 gold badge6 silver badges21 bronze badges 2
  • Save it to a variable when the previous event happens. – kapa Commented May 12, 2012 at 10:46
  • This is very little information, but usually what you are doing is saving the previous event since there is no history of events. – Amberlamps Commented May 12, 2012 at 10:47
Add a ment  | 

2 Answers 2

Reset to default 5

You can use .data() for this,

<ul class="menu">
   <li id="menu1">1</li>
   <li id="menu2">2</li>
   <li id="menu3">3</li>
   <li id="menu4">4</li>
   <li id="menu5">5</li>
</ul>​

$(".menu li").click(function() {
    if($(this).parent().data("lastClicked")){
        alert($(this).parent().data("lastClicked"));
    }
    $(this).parent().data("lastClicked", this.id);
});​

http://jsfiddle/aDUdq/

The variable lc is local. You must write instead :

var lc;
if(clicks > 1){
 lc = event.target.id;  // Last Clicked event
 }
console.log(lc);

本文标签: javascriptJquery last clickStack Overflow