简体   繁体   English

使用setTimeout延迟按钮上的样式更改

[英]Use setTimeout to delay a style change on a button

I would like to have a button styled normally. 我想要一个样式正常的按钮。 Then clicked, it will turn green. 然后单击,它将变为绿色。 Then after 3 seconds it will return to its normal style. 然后3秒钟后,它将恢复为正常样式。

I can get the button to change calling to my script with onClick but I get scope errors when I try to work in the function to return it to normal colors. 我可以使用onClick按钮来更改对脚本的调用,但是当我尝试使用该函数以将其恢复为正常颜色时遇到范围错误。

Javascript: Javascript:

<script type="text/javascript">

function onClickDelayEvent () {
    function newColor(elem) {
        elem.style.background = 'green';
        elem.style.color = 'white';
    }
    function normalColor(elem) {
        elem.style.background = '';
        elem.sytle.color = '';
    }
    setTimeout(normalColor,3000);
    }

</script>

HTML: HTML:

<button id="copyButton" onclick="onClickDelayEvent(this)">Copy</button>

The first function (newColor) doesnt fire and I get "elem" undefined on the normalColor function. 第一个函数(newColor)不会触发,并且我在normalColor函数上得到未定义的“ elem”。

If I strip out the extra code and just change the style once it works fine. 如果我删除了多余的代码,并且一旦工作正常就更改样式。 It's just getting the second part to flip it back that's not working. 只是将其翻转回去的第二部分是行不通的。

Is this you wanted? 这是你想要的吗?

You have typo elem.sytle.color = ''; 您有错字elem.sytle.color = ''; it should be style you need to call newColor function onClickDelayEvent 它应该是style你需要调用newColor功能onClickDelayEvent

 <script type="text/javascript"> function onClickDelayEvent (elem) { function newColor(elem) { elem.style.background = 'green'; elem.style.color = 'white'; } function normalColor(elem) { elem.style.background = ''; elem.style.color = 'black'; } setTimeout(normalColor,3000,elem); newColor(elem); } </script> <button id="copyButton" onclick="onClickDelayEvent(this)">Copy</button> 

You really don't need 3 functions here. 您这里确实不需要3个功能。 We can simplify this by just applying a CSS class upon click and then simply removing that class in a function that is delayed by 3 seconds. 我们可以通过在单击时应用CSS类,然后在延迟3秒的函数中删除该类来简化此操作。 This little "trick" is so handy because by removing a class, the element reverts back to whatever style was in effect before the class was added - - You don't have to do anything to get the old style back! 这个小技巧非常方便,因为通过删除一个类,该元素可以恢复为添加该类之前有效的样式--您无需执行任何操作即可恢复原来的样式!

Also, relying on the addition or removal of classes allows for multiple styles to be applied or removed at once, instead of working with individual styles. 同样,依靠类的添加或删除,可以立即应用或删除多个样式,而不是使用单个样式。

Lastly, do not use inline HTML event attributes ( onclick , etc.), here's why. 最后,请勿使用内联HTML事件属性( onclick等), 这就是原因。

 // Get button reference var btn = document.getElementById("copyButton"); // Set up event handler (do this in JavaScript, not HTML) btn.addEventListener("click", clickDelay); function clickDelay(evt) { evt.target.classList.add("special"); setTimeout(function(){ evt.target.classList.remove("special"); },3000); } 
 /* This class will be immediately added upon a click and then removed 3 seconds later, causing the button to return to its original style. */ .special { background-color:green; color:white; } 
 <button id="copyButton">Copy</button> 

You cloud probably receive a parameter to onClickDelayEvent and use it directly in the functions... something along the lines of 您的云可能会收到onClickDelayEvent的参数,并直接在函数中使用它……

<script type="text/javascript">

function onClickDelayEvent (elem) {
    function newColor() {
        elem.style.background = 'green';
        elem.style.color = 'white';
    }
    function normalColor() {
        elem.style.background = '';
        elem.sytle.color = '';
    }
    setTimeout(normalColor,3000);
}

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM