jQuery设置attr IE8错误

时间:2012-02-28 10:55:56

标签: jquery internet-explorer-8

我正在使用以下代码段

.append(
    $('<td>').append(
        $('<a>').attr({
        href: '#',
        class: 'assDelete',
                dataassetid: assetObj.ID
    }).append(
        $('<img>').attr({
        class: 'toolmeup',
                title: 'Delete Asset',
        src: '../_resources/images/icons/delete.png'
    })
))

然而,在类属性上的ie8扼流圈保存了“Experession Expected”

如果我删除该类代码正确执行???

修改 我可以使用'addClass'

解决这个问题

6 个答案:

答案 0 :(得分:8)

IE不喜欢class,因为它是保留的,var class = 123同样会失败,而是:

'class': 'toolmeup',

答案 1 :(得分:3)

classreserved keyword -- for possible future use!你应该用双引号(或单引号)括起来:

.append($('<td>').append($('<a>').attr({
    href: '#',
    "class": 'assDelete',
    dataassetid: assetObj.ID
}).append($('<img>').attr({
    "class": 'toolmeup',
    title: 'Delete Asset',
    src: '../_resources/images/icons/delete.png'
}))));​

答案 2 :(得分:1)

设置多个属性时,属性名称周围的引号是可选的。

警告:设置'class'属性时,必须始终使用引号!

http://api.jquery.com/attr/

答案 3 :(得分:0)

最后似乎缺少一个括号。

答案 4 :(得分:0)

用引号括起类

.append(
    $('').append(
        $('').attr({
        href: '#',
        'class': 'assDelete',
                dataassetid: assetObj.ID
    }).append(
        $('').attr({
        'class': 'toolmeup',
                title: 'Delete Asset',
        src: '../_resources/images/icons/delete.png'
    })
))`

答案 5 :(得分:0)

IE8在将类作为属性名称时出现问题。请尝试以下方法:

.append( 
    $('<td>').append( 
        $('<a>').attr({ 
        href: '#', 
        'class': 'assDelete', 
                dataassetid: assetObj.ID 
    }).append( 
        $('<img>').attr({ 
        'class': 'toolmeup', 
                title: 'Delete Asset', 
        src: '../_resources/images/icons/delete.png' 
    }) 
)) 
相关问题