DOM 属性访问:为什么“elt.class”是不工作?
我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
属性和属性是不同的东西。属性是您在文档代码中看到的字符串,任何类型的文档都可以通过 DOM 核心方法
getAttribute
:而可直接在对象上访问的属性是专门化的且特定于文档类型(例如在 DOM HTML 或 HTML5),通常是更具可读性的便利设施。尽管它们通常反映与属性相同的事物,但它们在名称、值、类型或用途方面可能有所不同。
因此,虽然
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
: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.
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 likeclass
(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 thedefaultValue
/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 usinggetAttribute
for accessing HTML DOM properties for this reason.]“class”是 JavaScript 中的保留字,因此 DOM 使用
className
来反映元素的当前类。这以字符串形式表示。例如:"foo bar baz"
(具有foo
、bar
和baz
类的元素)参见 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 classesfoo
,bar
, andbaz
)See https://developer.mozilla.org/en/DOM/element.className for examples of how to use it.
类是保留字。要访问类属性,请使用 className。
elt.className = "class_1";
Class is a reserved word. To access the class attribute use className.
elt.className = "class_1";