[英]jQuery element class not working anymore
I used to use the following syntax when using (previous versions of) jQuery: 在使用jQuery的先前版本时,我曾经使用以下语法:
jQuery('<span/>', {className: 'a'};
Using jQuery 1.6.2 the same will create classname
attribute on the element and not class
. 使用jQuery 1.6.2,将在元素而不是class
上创建classname
属性。 So I tried using (the reserved word) class
and it works, but only in (latest-ish) Chrome and Firefox. 因此,我尝试使用(保留字) class
并且class
有效,但仅在(最新的)Chrome和Firefox中有效。 IE 7 will choke on the reserved word. IE 7将阻塞保留字。
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()
: 您可以使用.addClass()
:
jQuery('<div/>').addClass('test').appendTo('body');
http://jsfiddle.net/userdude/yCzgs/ http://jsfiddle.net/userdude/yCzgs/
Or add it inline: 或内联添加:
jQuery('<div class="test2"/>').appendTo('body');
http://jsfiddle.net/userdude/yCzgs/1/ http://jsfiddle.net/userdude/yCzgs/1/
您可以只在关键字class
周围加上引号,因为它反过来更有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.