Javascript do something before alert

前端 未结 1 703
南旧
南旧 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)
    }
    Word .....

    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)
提交回复
热议问题