开发者

Cross browser solution for checking if a Javascript object is an html element

开发者 https://www.devze.com 2023-02-27 10:49 出处:网络
The following code work in FF but not IE8: var j = \"test\"; alert(j i开发者_开发技巧nstanceof HTMLElement);

The following code work in FF but not IE8:

var j = "test";
alert(j i开发者_开发技巧nstanceof HTMLElement);

I don't think IE uses the HTMLElement object. Is there a safe way to do this check in IE? Perhaps there is a YUI solution?


I use to check the nodeType property, it should be 1 for all HTML element objects.

I used it in my crossbrowser library before I switched to jQuery.

text has a nodeType of 3 and any custom objects probably won't have a nodeType property at all.


If you look at the jQuery source code (not the minified one ffs!), you'll see they make use of nodeType a lot.

0

精彩评论

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