87,921
社区成员
发帖
与我相关
我的任务
分享
<!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>
// more code
Object.prototype.isArrayLike.call(divs);
// more code