Animate()在1.2之前的jQuery版本中不起作用

时间:2014-06-16 17:29:16

标签: javascript jquery jquery-animate

当我在1.2之前使用jQuery版本时,animate()会导致错误:

未捕获的TypeError:undefined不是函数

从我阅读的文档中,它应该在早期版本中使用样式和持续时间参数,所以我觉得我一定做错了。

例如,以下代码正确运行:

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.2.js"></script>
<script language="Javascript" type="text/javascript">

function dotheStuff(){
    var div = document.createElement('div');
    div.style.width = '20px';
    div.style.height = '20px';
    div.style.backgroundColor = 'black';
    document.body.appendChild(div);

    $(div).animate({marginLeft:'600px'}, 2000);
}

</script>
</head>
<body onload="dotheStuff()">
</body>
</html>

虽然此代码产生错误:

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.1.4.js"></script>
<script language="Javascript" type="text/javascript">

function dotheStuff(){
    var div = document.createElement('div');
    div.style.width = '20px';
    div.style.height = '20px';
    div.style.backgroundColor = 'black';
    document.body.appendChild(div);

    $(div).animate({marginLeft:'600px'}, 2000);
}

</script>
</head>
<body onload="dotheStuff()">
</body>
</html>

唯一的区别是jQuery的版本。

非常感谢你。

1 个答案:

答案 0 :(得分:0)

我称之为一个错误,您可以通过删除单位并将您的值设为数字来解决:

$(div).animate({marginLeft: 600}, 2000);