本文介绍了单击N次后触发回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Java语言的新手,找不到我的问题的答案。如果您单击按钮5次后,我的JavaScript是否有可能显示div?如果是这种情况,我该怎么做呢?
I'm new in Javascript and I can't find the answer for my question. Is it possible that my javascript shows a div after if you clicked on a button 5 times? If this is the case, how can I make this possible?
谢谢!
推荐答案
没有jQuery:
document.addEventListener("DOMContentLoaded", function(event) {
var button = document.getElementById('click_me');
var elem = document.getElementById('message');
var count = 0;
button.addEventListener('click', function(e) {
e.preventDefault();
count++;
if(count == 5){
elem.style.display = 'block';
}
}, false);
});
#message {
background: #0f0;
display: none;
padding: 10px;
}
<button type="button" id="click_me">Click Me</button>
<div id="message">Hello World</div>
div>
使用jQuery:
$(function() {
var count = 0;
$('#click_me').click(function(e) {
e.preventDefault();
count++;
if(count == 5) {
$('#message').show();
}
});
});
#message {
background: #0f0;
display: none;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button type="button" id="click_me">Click Me</button>
<div id="message">Hello World</div>
这篇关于单击N次后触发回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!