简体   繁体   中英

How to check for HTMLDivElement in IE7

I have the following code;

if(data.constructor == HTMLDivElement) 

Now IE7 does not have/support HTMLDivElement.

How do I check for the same for IE7.

I tried

if(HTMLDivElement != undefined) 

But it still gives an error on IE7.

尝试:

if(data.constructor === document.createElement('div').constructor)

Since you are using jQuery, you can use a simpler test.

$('div#id').length == 0

That would be true when the element did not exist.

You could simply do it using jQuery's .is() method:

var isItDiv = $(data).is('div');

jsFiddle Demo :

//Using plain DOM elements - no cheating
var e1 = document.getElementById('elem1'); //<div id="elem1">
var e2 = document.getElementById('elem2'); //<button id="elem2">

//it does not even have to be in the DOM!
var ce = document.createElement('div');

console.log($(e1).is('div')); //true
console.log($(e2).is('div')); //false
console.log($(ce).is('div')); //true

I did something a kind simple like this:

if (data.tagName == 'DIV') {
    // do something
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM