jquery检查它是否被点击

$(element).click(function(){ }); 

如何检查元素是否被点击? 我这样做

 function element(){ $("#element").click(function(){ return 0; }); } if(element()==0){ alert("yes"); }else{ alert("no"); } 

但它没有返回任何东西。

你可以使用.data()

 $("#element").click(function(){ $(this).data('clicked', true); }); 

然后检查它:

 if($('#element').data('clicked')) { alert('yes'); } 

要获得更好的答案,您需要提供更多信息。

更新:

根据你的评论,我知道你想要的东西:

 $("#element").click(function(){ var $this = $(this); if($this.data('clicked')) { func(some, other, parameters); } else { $this.data('clicked', true); func(some, parameter); } }); 

使用jQuery,我会建议一个更短的解决方案。

 var elementClicked; $("element").click(function(){ elementClicked = true; }); if( elementClicked != true ) { alert("element not clicked"); }else{ alert("element clicked"); } 

(此处的“元素”将替换为实际的名称标签)

    
ravi

好吧,在我进入解决方案之前,让我们就这一事实站在同一条线上:Javascript是基于事件的。 因此,您通常必须设置回调才能执行程序。

根据你的评论我假设你有一个触发器,它会根据单击元素来执行启动函数的逻辑; 为了示范,我把它作为“提交按钮”; 但这可以是计时器或其他东西。

 var the_action = function(type) { switch(type) { case 'a': console.log('Case A'); break; case 'b': console.log('Case B'); break; } }; $('.clickme').click(function() { console.log('Clicked'); $(this).data('clicked', true); }); $('.submit').click(function() { // All your logic can go here if you want. if($('.clickme').data('clicked') == true) { the_action('a'); } else { the_action('b'); } }); 

实例: http : //jsfiddle.net/kuroir/6MCVJ/