1
下面的代码片段按预期工作:Deferred对象混乱
function boxAnimation() {
var dfd = $.Deferred();
$('div').fadeIn('slow', dfd.resolve);
return dfd.promise();
}
$(function() {
boxAnimation().done(
function() { $(this).animate({ 'margin-top': 50 }); },
function() { $(this).animate({ 'margin-left': 150 }); },
function() { $(this).animate({ 'margin-top': 250 }); },
function() { $(this).animate({ 'margin-left': 350 }); }
).fail(function() { alert('failed'); });
});
但是在这一个的递延对象既不拒绝或解决。
请告诉我,我该怎么错在何处。
function boxAnimation() {
var dfd = $.Deferred();
var randomNum = Math.floor(Math.random() * 5);
$('div').fadeIn('slow', function() {
if (randomNum == 1) {
dfd.reject;
}
else {
dfd.resolve;
}
});
return dfd.promise();
}
$(function() {
boxAnimation().done(
function() { $(this).animate({ 'margin-top': 50 }); },
function() { $(this).animate({ 'margin-left': 150 }); },
function() { $(this).animate({ 'margin-top': 250 }); },
function() { $(this).animate({ 'margin-left': 350 }); }
).fail(function() { alert('failed'); });
});
我的身体:
<div id='box' style='width:200px; height:200px; border:solid 1px #222222; display:none; background:#cccccc'></div>
有道理不过,我尝试过,但是这会导致错误(铬:无法读取属性未定义的“默认视图”)和fbug:a.ownerDocument是不确定的。你是否通过将它们转换为功能来实现它? – XGreen
@XGreen:我觉得你的DOM以某种方式参与,但我不知道你的HTML是什么样子。看起来你正在使用缩小版本;使用jQuery的unminified版本在调试时会有很多帮助。 – pimvdb
@XGreen:我错过了什么。你期望'this'是'div',但它是'Deferred'对象。如果拒绝/解决这个问题,你应该通过'this':http://jsfiddle.net/PrmQR/。 – pimvdb