开发者

DOM HTMLElement className property when the element has no class name set

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜