ie下object的prototype对nodelist无效???

sohighthesky 2009-10-24 05:39:34

<!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=gb2312" />
<title>无标题文档</title>
</head>
<body>
<div id="div1">
<div id="div2">a</div>
<div id="div3">b</div>
<div id="div4" class="b"></div>
<div id="div5">d</div>
</div>
</body>
<script type="text/javascript">
<!--
var g=function(s){return typeof(s)==="string"?document.getElementById(s):s;};

Object.prototype.isArrayLike=function() {
if(this instanceof Array)
return true;
if(/nodelist|htmlcollection/i.test(''+this))//chrome,safari,opera 为NodeList ,IE FF为HTMLCollection
return true;
if(this && (typeof this==='object') && ("length" in this) && (typeof this.length==='number') && this.length>0)//((this.length-1) in obj)
return true;
return false;
}

var obj={};
obj[0]=1;
obj[1]=2;
obj.length=3;
alert("object "+obj.isArrayLike());
var arr=[];
alert("array "+arr.isArrayLike());
var divs=document.getElementsByTagName("div");
alert("nodelist "+ divs.isArrayLike());

// -->
</script>
</html>


在非ie下都一路三个true,ie下最后一个却说“对象不支持此属性或方法”

此问题怎么解??
...全文
195 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dh20156 2009-10-30
  • 打赏
  • 举报
回复
只有构造函数的实例才能访问prototype链。

参见:http://www.jslab.org.cn/?tag=prototypeChain
sohighthesky 2009-10-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 objector 的回复:]
没啥办法,下面的方法巨土,不过是变通的方法:
JScript code// more codeObject.prototype.isArrayLike.call(divs);// more code
[/Quote]
哦,看样子只能把object当参数传进去 了,不用prototype了
Objector 2009-10-30
  • 打赏
  • 举报
回复
没啥办法,下面的方法巨土,不过是变通的方法:

// more code
Object.prototype.isArrayLike.call(divs);
// more code
fengsky491 2009-10-27
  • 打赏
  • 举报
回复
学习
sundotLei 2009-10-27
  • 打赏
  • 举报
回复
Dom Object没有prototype.
BeenZ 2009-10-24
  • 打赏
  • 举报
回复
好像IE对dom元素进行了保护,也就是说dom元素虽然是个object。却不能对object添加方法来达到对DOM元素有一样的效果

87,921

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧