主要是想判断是DOM还是JS对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" language="javascript">
// <![CDATA[
window.onload = function() {
var div = document.getElementsByTagName("DIV")[0];
alert(div.constructor.toString());
};
// ]]>
</script>
</head>
<body>
<div>sdf</div>
</body>
</html>在其他浏览器,包括ie8中都正常显示
但在IE6中却提示constructor为空或不是对象...
使用
Object.pretotype.toString.apply(div);
的方法,在其他品牌的浏览器也可以正常现实,但在IE中显示的却是“[object Object]”...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" language="javascript">
// <![CDATA[
window.onload = function() {
var div = document.getElementsByTagName("DIV")[0];
alert(div.constructor.toString());
};
// ]]>
</script>
</head>
<body>
<div>sdf</div>
</body>
</html>在其他浏览器,包括ie8中都正常显示
但在IE6中却提示constructor为空或不是对象...
使用
Object.pretotype.toString.apply(div);
的方法,在其他品牌的浏览器也可以正常现实,但在IE中显示的却是“[object Object]”...
不过现在大概知道怎么判断了...
var div = document.getElementsByTagName("DIV")[0];
if(typeof div.constructor !== "undefined") {
alert(div.constructor.toString());
} else {
alert("DOM");
}
还有其他办法吗??
最要是能像ie8一样显示“[object HTMLDivElement]”
if(div.nodeType == undefined) alert("不是dom对象")
else alert("是的")
<div id="div">abc</div>
<script>
var o = document.getElementById("div").constructor;
var s;
for(var i in o){
s += i + ": " + o[i] + "<br>";
}
document.write(s);
</script>
正想码代码
var div = {"nodeType":123};
突然想到“hasOwnProperty”这个方法
DOM对象没有这个方法,JS对象才有...
刚试验了一下...确切的说...应该是IE中,DOM对象没有这个方法...
手头上只能测试ie6、ie7、ie8,ie9不知道怎么样...
var div = document.getElementsByTagName("DIV")[0];
function fun(o)
{
var v = ""
var type = typeof(o.constructor); switch(type)
{
case "object":
v = "dom";
break;
case "function":
v="javascriptobject" ;
break;
default :
v ="dom";
break;
}
return v;
}alert(fun(o))
alert(fun(div))