如何仅使用javascript在短时间内更改单击按钮上的文本? [英] How to change the button text on click for a short duration only using javascript?
本文介绍了如何仅使用javascript在短时间内更改单击按钮上的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在建立一个购物车网站,我希望我的添加到购物车按钮在点击该按钮时说已添加商品,但仅需2秒钟,然后它就会更改返回添加到购物车.我该如何做到这一点?
I'm making a shopping cart website and I want my Add to Cart button to say Item added upon clicking it, but only for like 2 seconds then it changes back to Add to Cart . How do I do achieve this?
推荐答案
在普通的Javascript中,您可以使用变量来检查是否单击了按钮,如果没有单击,则将按钮设置为所需的字符串,并在两次输入后将其更改回秒.
In plain Javascript, you could use a variable for checking if the button is clicked and if not, set the button to the wanted string and change it back after two seconds.
document.getElementById('button').addEventListener('click', function (clicked) {
return function () {
if (!clicked) {
var last = this.innerHTML;
this.innerHTML = 'Item added';
clicked = true;
setTimeout(function () {
this.innerHTML = last;
clicked = false;
}.bind(this), 2000);
}
};
}(false), this);
<button id="button">Add to Cart</button>
这篇关于如何仅使用javascript在短时间内更改单击按钮上的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文