jQuery 检查它是否被点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6081608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery check if it is clicked or not
提问by gambozygame
$( element ).click( function() {
});
How can I check if the element is clicked or not? I'm doing like that
如何检查元素是否被点击?我就是这样做的
function element() {
$( "#element" ).click( function() {
return 0;
} );
}
if( element() == 0 ) {
alert( "yes" );
} else {
alert( "no" );
}
But it's not returning anything.
但它没有返回任何东西。
回答by Felix Kling
You could use .data()
:
你可以使用.data()
:
$("#element").click(function(){
$(this).data('clicked', true);
});
and then check it with:
然后检查它:
if($('#element').data('clicked')) {
alert('yes');
}
To get a better answer you need to provide more information.
要获得更好的答案,您需要提供更多信息。
Update:
更新:
Based on your comment, I understand you want something like:
根据您的评论,我了解您想要以下内容:
$("#element").click(function(){
var $this = $(this);
if($this.data('clicked')) {
func(some, other, parameters);
}
else {
$this.data('clicked', true);
func(some, parameter);
}
});
回答by grifare -- Armagan Tekdoner
Using jQuery, I would suggest a shorter solution.
使用 jQuery,我会建议一个更短的解决方案。
var elementClicked;
$("element").click(function(){
elementClicked = true;
});
if( elementClicked != true ) {
alert("element not clicked");
}else{
alert("element clicked");
}
("element" here is to be replaced with the actual name tag)
(此处的“元素”将替换为实际的名称标签)
回答by asdf
<script>
var listh = document.getElementById( 'list-home-list' );
var hb = document.getElementsByTagName('hb');
$("#list-home-list").click(function(){
$(this).style.color = '#2C2E33';
hb.style.color = 'white';
});
</script>
回答by trickymind
This is the one that i've tried & it works pretty well for me
这是我尝试过的,对我来说效果很好
$('.mybutton').on('click', function() {
if (!$(this).data('clicked')) {
//do your stuff here if the button is not clicked
$(this).data('clicked', true);
} else {
//do your stuff here if the button is clicked
$(this).data('clicked', false);
}
});
for more reference check this link JQuery toggle click
有关更多参考,请检查此链接 JQuery 切换单击
回答by Ravi Parekh
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
var val;
$(document).ready(function () {
$("#click").click(function () {
val = 1;
get();
});
});
function get(){
if (val == 1){
alert(val);
}
}
</script>
<table>
<tr><td id='click'>ravi</td></tr>
</table>
回答by MarioRicalde
Alright, before I go into the solution, lets be on the same line about this one fact: Javascript is Event Based. So you'll usually have to setup callbacks to be able to do procedures.
好的,在我进入解决方案之前,让我们就这一事实保持一致:Javascript 是基于事件的。因此,您通常必须设置回调才能执行程序。
Based on your comment I assumed you have a trigger that will do the logic that launched the function depending if the element is clicked; for sake of demonstration I made it a "submit button"; but this can be a timer or something else.
根据您的评论,我假设您有一个触发器,可以根据是否单击元素来执行启动该功能的逻辑;为了演示起见,我将其设为“提交按钮”;但这可以是计时器或其他东西。
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');
}
});
Live Example: http://jsfiddle.net/kurtheitroad/6MCVJ/