0

我确定这个问题已经被问过了,但我找不到合适的关键字来喂谷歌。我什至找不到我的问题的好标题。我想做这个 :

$(".selector").click(function() {
    $(this).css("background-color", "red");
    if (confirm("Do you want to do this ?") {
        // do this
    } else {
        $(this).css("background-color", "transparent");
    }
});

但是只有在我确认后才会触发 css 更改。如何在确认之前(或在确认期间)触发它?

谢谢。

4

1 回答 1

1

我敢打赌,这只是添加延迟(甚至只是 0 毫秒)的简单问题。这将确保页面在确认消息出现之前重新绘制。

$(".selector").click(function() {
    $(this).css("background-color", "red");
    var _this = this;
    function confirmCode(){
        if (confirm("Do you want to do this ?") {
            // do this
        } else {
            $(_this).css("background-color", "transparent");
        }
    }
    setTimeout(function(){confirmCode()}, 0)
});

编辑:添加完整代码

于 2012-05-05T13:46:37.653 回答