Javascript do something before alert

前端 未结 1 704
南旧
南旧 2020-12-11 07:33

I want to change the color and alert a message when a button is clicked.

function colorChange() {
  document.getElementById(\'word\').style.color = \"red\";
         


        
相关标签:
1条回答
  • 2020-12-11 08:09

    Wrap the alert in a timeout

    function colorChange() {
      document.getElementById('word').style.color = "red";
      setTimeout(function() {
      	alert("color changed!");
      },10)
    }
    <div id="word">
    Word .....
    </div>
    <br />
    <button onclick="colorChange()">
    Change color
    </button>

    The browser doesn't have time to repaint before the alert fires, by using a timeout you delay the alert until the next tick, when the repaint has completed.

    0 讨论(0)
提交回复
热议问题