最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't set "class" attribute? - Stack Overflow

programmeradmin2浏览0评论

These lines in jquery making me bad:

var selectBoxContainer = $('<div>',{width: select.outerWidth(), className:'styledSelect', html:'<div class="selectBox"></div>'});
var dropDown = $('<ul>',{className:'dropDown'});

It should set the class attribute "class="styledselect" in the output, but the attribute's name is "classname":

<div classname="styledSelect">
    <ul classname="dropDown" style="display: none;">...</ul>
</div>

when I change it simply to {class:'dropDown'} ist works in firefox, but not in other browsers.

help please...

These lines in jquery making me bad:

var selectBoxContainer = $('<div>',{width: select.outerWidth(), className:'styledSelect', html:'<div class="selectBox"></div>'});
var dropDown = $('<ul>',{className:'dropDown'});

It should set the class attribute "class="styledselect" in the output, but the attribute's name is "classname":

<div classname="styledSelect">
    <ul classname="dropDown" style="display: none;">...</ul>
</div>

when I change it simply to {class:'dropDown'} ist works in firefox, but not in other browsers.

help please...

Share Improve this question edited Nov 18, 2011 at 9:21 user166390 asked Nov 18, 2011 at 9:11 ThomaszterThomaszter 1,4741 gold badge13 silver badges26 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 11

Try:

{ "class": "dropDown" }

or add .addClass("drowDown") to the end.

Use

{'class': 'dropDown'}

class is a future reserved word in JavaScript, so you must specify it as a string inside object literals for some browsers, notably IE 8 and lower.

The simplest way would to use the following:

var dropDown = $('<ul>').addClass('dropDown');

Use the jQuery native methods, they're made for this. It's also much more readable this way.

{'class':'dropDown'}

should work in all browsers. Alternatively, you could try

dropDown.addClass("dropDown");

Good luck!

发布评论

评论列表(0)

  1. 暂无评论