如何知道元素是否已被点击jquery

Sad*_*tam 6 jquery events click

我有一个场景,我点击一个元素说#stationLink.当我再次点击它时,我想知道该元素是否已被点击.我试过了

var a=false;
$("#stationLink").click(function(){
   $(this).click(function(){
     a = true
   });
   console.log(a);
});
Run Code Online (Sandbox Code Playgroud)

false只得到两次true......我想我错过了一些东西.或者还有其他方法吗?

Gab*_*oli 9

这应该做你想要的(包括保留我在你的评论中看到你想要的一个计数器)

$("#stationLink").click(function(e){
   var $this = $(this);
   var clickCounter = $this.data('clickCounter') || 0;
   // here you know how many clicks have happened before the current one

   clickCounter += 1;
   $this.data('clickCounter', clickCounter);
   // here you know how many clicks have happened including the current one

});
Run Code Online (Sandbox Code Playgroud)

使用该.data()方法将计数器与DOM元素一起存储,这样就可以将相同的处理程序应用于多个元素,因为每个元素都有自己的计数器.

演示http://jsfiddle.net/gaby/gfJj6/1/