代码的意思应该是 先变成红色,再弹1。但是现在反过来了。。。<style> #box{ width: 100px; height: 100px; background-color: #7FFFD4; }</style><body> <div id="box"></div> <button>123123</button> </body><script> $("button").click(function() { $("#box").css("background-color", "red"); alert(1) })</script>
2 回答

冉冉说
TA贡献1877条经验 获得超1个赞
执行顺序其实没错,只是 alert() 会阻塞页面更新。
$("#box").css("background-color", "red") // 这行先执行了
alert(1) // 弹出框,页面不更新了,此时 css 其实已经改了,但是页面没有更新所以你看不到变成红色
补充
解决方法很简单,用 setTimeout 就行了
$("#box").css("background-color", "red")
setTimeout(() => {
alert(1)
}, 0)
添加回答
举报
0/150
提交
取消