Jquery css()不';t在Safari中的下一个语句之前完成
我有一段代码:Jquery css()不';t在Safari中的下一个语句之前完成,jquery,safari,timing,Jquery,Safari,Timing,我有一段代码: $(this).css('color', 'red'); if(confirm('Do you want to delete this item?')) window.location.href = link; else $(this).css('color', 'black'); 但不幸的是,在调用confirm()之前,css('color',red')不会执行。如果我使用animate而不是css,并在回调函数中执行confirm(),它会在动画实际完成之
$(this).css('color', 'red');
if(confirm('Do you want to delete this item?'))
window.location.href = link;
else
$(this).css('color', 'black');
但不幸的是,在调用confirm()之前,css('color',red')不会执行。如果我使用animate而不是css,并在回调函数中执行confirm(),它会在动画实际完成之前执行
注意:这个问题没有出现在chrome中,在chrome中它可以正常工作(对我来说)。确认对话框可能会阻止页面更新,直到页面关闭。要避免这种情况,请在打开浏览器之前给它一些时间:
var $this = $(this); // in the callback we cannot use `this`
$this.css('color', 'red');
window.setTimeout(function() {
if(confirm('Do you want to delete this item?'))
window.location.href = link;
else
$this.css('color', 'black');
}, 0);
小超时应该有帮助:
var el = $(this);
el.css('color', 'red');
setTimeout(function() {
if(confirm('Do you want to delete this item?'))
window.location.href = link;
else
el.css('color', 'black');
}, 1);
演示:在if语句中,这是否适用于您: 同意测试人员的意见-firebug太宽大了
var $this = false;
if($this = $(this).css('color', 'red')) {
if(confirm('Do you want to delete this item?')) {
window.location.href = link;
} else {
$this.css('color', 'black');
}
}
可以随意编辑吗?我错过了什么它的计算结果应该为true(不是比较)。您没有从
if
语句中删除var
,并在之前定义$this
。在语句中定义var仅在操作的右侧完成时才可用。此语法有效,不会导致错误。此外,它还要求将结果分配给变量,因此它要求css在返回之前完成,然后在调用confirm()之前创建执行中断-理论上。我无法在safari上复制原始错误。您可以尝试以前的版本,它会引发类似未捕获语法错误:意外标记var
.Ah=)的问题,这取决于我在什么模式下执行firebug-请测试人员注意-编辑答案。谢谢在JSFIDLE中似乎可以工作,但在我的页面上,它有时工作,有时不工作。间隔10次就可以了,但我想这取决于每台访问者的电脑?与VisioN的答案类似,这有时有效,有时无效-对我来说。你考虑过使用基于html的确认对话框而不是本机对话框吗?这不会有这些缺点。是的,这是一个好主意,只是这个问题引起了我的兴趣,所以我想在制定解决方案之前,尝试找到一个解决方案;)