如何确保按钮事件只在Javascript / JQuery中触发一次?(How to make sure buttons events are only triggered once in Javas

编程入门 行业动态 更新时间:2024-10-27 00:31:53
如何确保按钮事件只在Javascript / JQuery中触发一次?(How to make sure buttons events are only triggered once in 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'); } });

最满意答案

我会在JavaScript中绑定click事件并使用jQuery .one()方法。

文件

基本上,一种方法允许您绑定只能触发一次的事件,例如单击事件。

现场演示

var percentage = 0; $('ul li a').one('click', updateBar); function updateBar() { percentage += 25; $('#test').text(percentage); }

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

var percentage = 0; $('ul li a').one('click', updateBar); function updateBar() { percentage += 25; $('#test').text(percentage); }

更多推荐

本文发布于:2023-04-29 11:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:只在   按钮   事件   Javascript   triggered

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!