开发者

jQuery element class not working anymore

I used to use the following syntax when using (previous versions of) jQuery:

jQuery('<span/>', {className: 'a'};

Using jQuery 1.6.2 the same will create classname attribute on the element and not class. So I tried using (the reserved word) class and it works, but only in (latest-ish) Chrome and Firefox. IE 7 will choke on the reserved word.

What am I missing here? The (truncated) script is below:

jQuery('<div/>', {
    className: 'wrap'
})
.append(jQuery('<div/>', {
    className: 'button',
    html: jQuery('<a/>', {
        href: function() {
            return '#'
   开发者_如何学编程     }(),
        className: 'amaranth',
        html: '<span>View in</span> Site'
    })
}))
.appendTo(that);


You can use .addClass():

jQuery('<div/>').addClass('test').appendTo('body');

http://jsfiddle.net/userdude/yCzgs/

Or add it inline:

jQuery('<div class="test2"/>').appendTo('body');

http://jsfiddle.net/userdude/yCzgs/1/


You can just put quotes around the keyword class, which is more valid JSON anyway

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜