当元素没有设置类名时,DOM HTMLElement className 属性

发布于 2024-10-07 22:07:16 字数 164 浏览 1 评论 0原文

当您有一个未设置类名的 HTML 元素时,HTMLElement className 属性的值是多少?我的第一个想法是它是未定义的,但我发现在 FF 中它只是一个空字符串。我的问题是 - 我可以在所有当前和未来的浏览器中依赖这种行为吗?这是某些规范的一部分,还是只是另一个浏览器技巧,以避免编写糟糕的代码中的错误?

When you have an HTML element with no class name set, what is the value of HTMLElement className property? My first thought was that it is undefined, but I found out that in FF it is just an empty string. My question is - can I rely on this behavior in all current and future browsers? Is that part of some specification or is it just another browser trick to avoid errors in badly written code?

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

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

发布评论

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

评论(2

橘虞初梦 2024-10-14 22:07:16

默认情况下,它始终为空字符串(专门是DOMString),是的,您可以相信它不是null/undefined

真正重要的是 getAttribute( )定义

返回值 DOMString
Attr 字符串形式的值,如果该属性没有指定值或默认值,则为空字符串。


...这实际上只是您要撤回的属性,因此在两种情况下都是相同的行为,接口只是指定这些属性。

It will always by default be an empty string (a DOMString specifically), and yes you can rely on it not being null/undefined.

Where it really matters is the getAttribute() definition:

Return Value DOMString
The Attr value as a string, or the empty string if that attribute does not have a specified or default value.

...it's really just an attribute you're pulling back, so it's the same behavior in both cases, the interface just specifies those attributes.

爱殇璃 2024-10-14 22:07:16
interface HTMLElement : Element {
           attribute  DOMString            id;
           attribute  DOMString            title;
           attribute  DOMString            lang;
           attribute  DOMString            dir;
           attribute  DOMString            className;
};

http://www. w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-html.html

另请参阅 DOMString

所以应该是安全的。

interface HTMLElement : Element {
           attribute  DOMString            id;
           attribute  DOMString            title;
           attribute  DOMString            lang;
           attribute  DOMString            dir;
           attribute  DOMString            className;
};

http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-html.html

And also see the definition of DOMString.

So it should be safe.

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