jQuery 从版本升级时 JS 不再工作

jQuery JS no longer works when upgrading from version

当我将以下 JS 函数从 jQuery 1.4.2 更新到最新的 1.11.2 时,className Steam 不再具有动画效果。

$(window).load(function(){
    function animSteam(){
        $('<span>',{
            className:'steam'+Math.floor(Math.random()*2 + 1),
            css:{
                marginLeft    : -10 + Math.floor(Math.random()*20)
            }
        }).appendTo('#rocket').animate({
            left:'-=58',
            bottom:'-=100'
        }, 120,function(){
            $(this).remove();
            setTimeout(animSteam,10);
        });
    }
    function moveRocket(){
        $('#rocket').animate({'left':'+=100'},5000).delay(1000)
        .animate({'left':'-=100'},5000,function(){
            setTimeout(moveRocket,1000);
        });
    }
    moveRocket();
    animSteam();
});

这是一个很酷的动画,我不想松开它,我不确定哪个功能已被弃用。

The name "class" must be quoted in the object since it is a JavaScript reserved word, and "className" cannot be used since it refers to the DOM property, not the attribute.

参考:http://api.jquery.com/jQuery/#jQuery-html-attributes

看看这个:http://bugs.jquery.com/ticket/9150#comment:1

That was never supported and was coincidental that it worked. The attribute to set is class, not className.

看来你要的是:

    $('<span>',{
        'class': 'steam'+Math.floor(Math.random()*2 + 1),
        css:{
            marginLeft    : -10 + Math.floor(Math.random()*20)
        }
    })