开发者

Detecting if argument is Javascript HTML Element datatype

开发者 https://www.devze.com 2022-12-19 05:26 出处:网络
I need a XB (Cross Browser) method of detecting if an argument is a HTML Element. Using the following code gives different results in different browsers:

I need a XB (Cross Browser) method of detecting if an argument is a HTML Element.

Using the following code gives different results in different browsers:

Object.prototype.t开发者_运维百科oString.call(element);
// returns in FF "[object HTMLDivElement]";
// returns in IE "[object Object]";

The other method I found was:

if(element.nodeType)  // true for a HTML Element;

Does someone knows a XB tested solution?


You want this:

if (element.nodeType === element.ELEMENT_NODE)
// Element.prototype.ELEMENT_NODE === 1

if (element.nodeType) is almost always true. For example, the nodeType of a comment is 8, so it would be detected as an element with your code even though it isn't.

0

精彩评论

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

关注公众号