第三次点击div后如何创建预警?我已经尝试了一些示例,但未能使其工作。
以下是到目前为止的代码(不包括count部分):
$("div").one( "click", function() {
$(this).css({
background: "red",
cursor: "auto",
});
$(this).append( "0" );
$("span").append( "0" );
});
小提琴http://jsfiddle.net/7gosayyo/
发布于 2015-07-09 05:47:42
需要使用一些JavaScript来跟踪它被点击的次数。
<button onclick="clicking()>Click me</button>
var nClicks = 0;
var nMaxClicks = 3;
function Clicking() {
// increment the counter each time item is clicked
nClick++;
// On the 3rd click....
if (nClicks == nMaxClicks){
// Enter code for thing you are looking to do
alert("Max clicks hit! " + nClicks);
}
}
发布于 2015-07-09 05:47:06
解决方案
我们给div分配了一个新的属性,它保存了我们被点击的时间(以保存一个额外的全局变量)。一旦是三次,我们就会显示我们的警告消息。
<html>
<head>
<script>
function clickMe(e){
e.clicked = (e.clicked || 0) + 1;
if (e.clicked === 3) alert('Three times'); //Replace with (e.clicked % 3 === 0) if required.
}
</script>
</head>
<body>
<div onclick = 'clickMe(this)'>Click me three times</div>
</body>
</html>
如果您想在每次单击第三次时获得警报(因此也可以在第六次、第九次、..)将该行替换为以下内容:
if (e.clicked % 3 === 0) alert('Three times');
编辑
应请求添加了一个提问器:https://jsfiddle.net/19jy296L/
发布于 2015-07-09 05:47:21
这就是你所期待的吗?
http://jsfiddle.net/b1h4y9xL/
var click_count = 0;
$("div").on( "click", function() {
click_count++;
$(this).css({
background: "red",
cursor: "auto",
});
$(this).append( "0" );
$( "span" ).append( "0" );
if(click_count == 3){
click_count = 0;
alert('You have clicked three times!');
}
});
你可以跟踪一个框在一个变量中被点击了多少次,当它被点击到三次时,你可以重新设置它。然后,每次你点击,它只是检查你是否得到了你的三次点击。
https://stackoverflow.com/questions/31304334
复制相似问题