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

javascript - DOM attribute access: why is "elt.class" not working? - Stack Overflow

programmeradmin1浏览0评论

I have this javascript code:

var elt = document.createElement("div");
elt.class = "class_1";

There is supposed to be a styling associated with .class_1 in my CSS, but it did not really apply for some reason. After spent a few hours figuring out what went wrong, I tried this alternative:

elt.setAttribute("class", "class_1");

and it worked....

It is weird since on the other part of my code, I used elt.id and it worked just fine. At first I thought it was a cross-browser issue, but it turned out that "elt.class" doesnt work in all browser.

Is this a bug in the native javascript DOM? can somebody explain why this is or if I did anything wrong? Thanks. All inputs/answers would be appreciated.

I have this javascript code:

var elt = document.createElement("div");
elt.class = "class_1";

There is supposed to be a styling associated with .class_1 in my CSS, but it did not really apply for some reason. After spent a few hours figuring out what went wrong, I tried this alternative:

elt.setAttribute("class", "class_1");

and it worked....

It is weird since on the other part of my code, I used elt.id and it worked just fine. At first I thought it was a cross-browser issue, but it turned out that "elt.class" doesnt work in all browser.

Is this a bug in the native javascript DOM? can somebody explain why this is or if I did anything wrong? Thanks. All inputs/answers would be appreciated.

Share Improve this question edited Jul 12, 2011 at 21:05 user1385191 asked Jul 12, 2011 at 20:33 Benny TjiaBenny Tjia 4,88310 gold badges41 silver badges49 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

Properties and attributes are different things. Attributes are the strings you see in the document code, accessible for any type of document through the DOM Core method getAttribute:

<a id="foo" href="bar" tabindex="1" onclick="alert()" class="bof" potato="lemon">

a.getAttribute('id')         // string "foo"
a.getAttribute('href')       // string "bar"
a.getAttribute('tabindex')   // string "1"
a.getAttribute('onclick')    // string "alert()"
a.getAttribute('class')      // string "bof"
a.getAttribute('potato')     // string "lemon"

whereas the properties, accessible directly on the object, are specialised and document-type-specific (eg defined in DOM HTML or HTML5) and are generally more readable convenience facilities. Although they often reflect the same thing as the attribute, they can differ in terms of name, value, type or purpose.

a.id                         // string "foo"
a.href                       // string "http://www.example./base/bar"
a.tabIndex                   // integer 1
a.onclick                    // function() { alert(); }
a.className                  // string "bof"
a.potato                     // undefined

So whilst id results in the same value, href and all other URL properties get absolutised relative to the document; tabIndex and other numeric properties returns a Number instead of literal string; onclick and other event handler properties return a JS Function object, and properties whose name happens to clash with mon reserved words like class (not just only JavaScript reserved words, as DOM is a cross-language standard) get renamed (see also: htmlFor), and non-standard attributes aren't accessible as properties at all. Other gotchas: value/selected/checked on inputs reflect the current contents of a form field; the attributes of the same name are the defaultValue/defaultSelected/defaultChecked initial values.

[Aside: IE<8 has real trouble telling the difference between attributes and properties, and will return the wrong thing for getAttribute in all cases where they differ. Don't let this confuse you, and avoid using getAttribute for accessing HTML DOM properties for this reason.]

"class" is a reserved word in JavaScript, so the DOM uses className to reflect the element's current class(es). This is represented in string form. Ex: "foo bar baz" (an element with classes foo, bar, andbaz)

See https://developer.mozilla/en/DOM/element.className for examples of how to use it.

var elt = document.createElement("div");
elt.className = "class_1";

Class is a reserved word. To access the class attribute use className. elt.className = "class_1";

发布评论

评论列表(0)

  1. 暂无评论