如何确保 Javascript/JQuery 中的按钮事件仅触发一次?
我是 JQuery 和 Javascript 的新手,我正在使用 JQueryUI ProgressBar,每次有人单击按钮时,进度条就会动画并填满。有没有办法跟踪哪些按钮已被按下,以防止按钮再次触发事件?
当前 HTML:
<ul>
<li><a href="left/section1.html" target="leftcontainer" onClick="updateBar();window.presentation.location.href='right/section1/page1.html';;return true;">overview</a></li>
<li><a href="left/section2.html" target="leftcontainer" onClick="updateBar();window.presentation.location.href='right/section1/page1.html';;return true;">key features</a></li>
<li><a href="#" onClick="updateBar()">customers</a></li>
<li><a href="#" onClick="updateBar()">accessories</a></li>
<!--<li><a href="#">nav5</a></li>
<li><a href="#">nav6</a></li> -->
</ul>
当前 JQuery/JavaScript:
var percentage = 0;
function updateBar()
{
percentage += 25;
$('.ui-progressbar-value').stop().animate({ width: percentage+"%" }, 500)
$('.middle').progressbar('option','value', percentage);
if(percentage > 100)
{
$('ui-progressbar-value').stop().animate({ width: "100%"}, 500);
}
}
$(function() {
$('.middle').progressbar({
value: percentage,
complete: function() { alert('The progress bar is full'); }
});
I'm new to JQuery, and Javascript, and I am using the JQueryUI ProgressBar, and every time somebody clicks on a button, then the progress bar animates and fills up. Is there a way to keep track of which buttons are already pressed to prevent the buttons from triggering the event again?
Current HTML:
<ul>
<li><a href="left/section1.html" target="leftcontainer" onClick="updateBar();window.presentation.location.href='right/section1/page1.html';;return true;">overview</a></li>
<li><a href="left/section2.html" target="leftcontainer" onClick="updateBar();window.presentation.location.href='right/section1/page1.html';;return true;">key features</a></li>
<li><a href="#" onClick="updateBar()">customers</a></li>
<li><a href="#" onClick="updateBar()">accessories</a></li>
<!--<li><a href="#">nav5</a></li>
<li><a href="#">nav6</a></li> -->
</ul>
Current JQuery/JavaScript:
var percentage = 0;
function updateBar()
{
percentage += 25;
$('.ui-progressbar-value').stop().animate({ width: percentage+"%" }, 500)
$('.middle').progressbar('option','value', percentage);
if(percentage > 100)
{
$('ui-progressbar-value').stop().animate({ width: "100%"}, 500);
}
}
$(function() {
$('.middle').progressbar({
value: percentage,
complete: function() { alert('The progress bar is full'); }
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将在 JavaScript 中绑定单击事件并使用 jQuery
.one()
方法。文档
基本上,one 方法允许您绑定只能触发一次的事件,例如单击事件。
现场演示
I would bind the click event in the JavaScript and use the jQuery
.one()
method.documentation
Basically the one method allows you to bind an event that can only be triggered once, such as a click event.
Live Demo
HTML(添加类):
JS
HTML (add a class):
JS