DOM 属性访问:为什么“elt.class”是不工作?

发布于 2024-11-19 23:52:46 字数 494 浏览 1 评论 0原文

我有这个 javascript 代码:

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

在我的 CSS 中应该有一个与 .class_1 相关的样式,但由于某种原因它并没有真正应用。花了几个小时弄清楚出了什么问题后,我尝试了这个替代方案:

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

它起作用了......

这很奇怪,因为在我的代码的另一部分,我使用了 elt.id 并且它起作用了很好。起初我以为这是一个跨浏览器的问题,但事实证明 "elt.class" 并不能在所有浏览器中工作。

这是原生 javascript DOM 中的错误吗?有人可以解释为什么会这样或者我是否做错了什么?谢谢。所有意见/答案将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的往事 2024-11-26 23:52:46

属性和属性是不同的东西。属性是您在文档代码中看到的字符串,任何类型的文档都可以通过 DOM 核心方法 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"

而可直接在对象上访问的属性是专门化的且特定于文档类型(例如在 DOM HTMLHTML5),通常是更具可读性的便利设施。尽管它们通常反映与属性相同的事物,但它们在名称、值、类型或用途方面可能有所不同。

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

因此,虽然 id 产生相同的值,但 href 和所有其他 URL 属性相对于文档而言是绝对化的; tabIndex 和其他数字属性返回数字而不是文字字符串; onclick 和其他事件处理程序属性返回一个 JS Function 对象,并且属性的名称恰好与常见的保留字(如 class)冲突(不仅仅是 JavaScript 保留字,如 DOM跨语言标准)被重命名(另请参阅:htmlFor),并且非标准属性根本无法作为属性访问。其他问题:输入中的value/selected/checked反映了表单字段的当前内容;同名的属性是 defaultValue/defaultSelected/defaultChecked 初始值。

[旁白:IE<8 确实很难区分属性和属性之间的差异,并且在它们不同的所有情况下都会为 getAttribute 返回错误的内容。不要让这让您感到困惑,并避免使用 getAttribute 来访问 HTML DOM 属性。]

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.com/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 common 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.]

美人如玉 2024-11-26 23:52:46

“class”是 JavaScript 中的保留字,因此 DOM 使用 className 来反映元素的当前类。这以字符串形式表示。例如:"foo bar baz"(具有 foobarbaz 类的元素)

参见 https://developer.mozilla.org/en/DOM/element.className 例如如何使用它。

"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.org/en/DOM/element.className for examples of how to use it.

乖乖哒 2024-11-26 23:52:46
var elt = document.createElement("div");
elt.className = "class_1";
var elt = document.createElement("div");
elt.className = "class_1";
凉宸 2024-11-26 23:52:46

类是保留字。要访问类属性,请使用 className。 elt.className = "class_1";

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文