【问题标题】:jQuery JS no longer works when upgrading from version从版本升级时 jQuery JS 不再工作
【发布时间】:2015-02-04 02:40:50
【问题描述】:

当我将以下 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();
});

这是一个很酷的动画,我讨厌放弃,我不确定哪些功能已被弃用。

【问题讨论】:

  • 您在调试控制台中看到任何错误吗?

标签: jquery deprecated


【解决方案1】:

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

这从未得到支持,而且很巧合。要设置的属性是类,而不是类名。

所以,看来你想要的是:

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

【讨论】:

  • 谢谢你的作品。当你使用别人的代码而不知道它实际上是如何工作时,就会发生这种情况。图形设计师感谢合法的编码器:)
【解决方案2】:

名称“class”必须在对象中引用,因为它是 JavaScript 保留字,并且不能使用“className”,因为它指的是 DOM 属性,而不是属性。

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

【讨论】:

    猜你喜欢
    • 2019-07-22
    • 2020-05-09
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2022-12-02
    • 1970-01-01
    相关资源
    最近更新 更多