getElementsByTagName返回的数组怎么不支持数组方法?

MuBeiBei 2009-09-25 02:16:05

function process(){
alert('aaa');
};
window.onload = function(){
var oInputs = document.getElementsByTagName('input');
var array = ['a','b'];
chunk(oInputs,process); //这块传array数组就可以,传oInputs就不行报错!
};
function chunk(array,process,context){
var items = array.concat();
setTimeout(function(){
var ite = items.shift();
process.call(context,ite);

if(items.length > 0){
setTimeout(arguments.callee,100);
}
},100);
};



<input type="button" id="testx" value="aa" />
<input type="button" id="testy" value="bb" />
<input type="button" id="testz" value="cc" />
<input type="button" id="testa" value="dd" />
...全文
533 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
yang_kuan 2009-10-08
  • 打赏
  • 举报
回复
mark
MuBeiBei 2009-09-25
  • 打赏
  • 举报
回复
结贴了~···感谢大家~···
MuBeiBei 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 s_liangchao1s 的回复:]
JScript code// 比如 arguments<script type="text/javascript"><!--function Pro(){
alert([].slice.call(arguments));// or Array.prototype.slice.call(arguments) }

Pro('1','2','3')//--></script>
[/Quote]

嗯 我明白了~·谢谢~·
s_liangchao1s 2009-09-25
  • 打赏
  • 举报
回复

// 比如 arguments
<script type="text/javascript">
<!--
function Pro(){
alert([].slice.call(arguments));// or Array.prototype.slice.call(arguments)
}

Pro('1','2','3')

//-->
</script>

s_liangchao1s 2009-09-25
  • 打赏
  • 举报
回复

我看错题目了 想当然给你写的....等回头一看 你是将Object类型转成数组...
MuBeiBei 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 s_liangchao1s 的回复:]
HTML code
// 不好意思 我看错题<scripttype="text/javascript"><!--var $A=function(coll){if(coll.item){var l= coll.length, array=new Array(l);while (l--) array[l]= coll[l];return array;
}
}
window.onload=function(){var oInputs= document.getElementsByTagName('input');
alert($A(oInputs))
};//--></script><inputtype="button" id="testx" value="aa"/><inputtype="button" id="testy" value="bb"/><inputtype="button" id="testz" value="cc"/><inputtype="button" id="testa" value="dd"/>
[/Quote]

可以了~··我还想问你下~·你之前跟我说的那个方法怎么才能让它在IE下也支持呢?
就是这个Array.prototype.slice.call(oInputs)
s_liangchao1s 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 mubeibei 的回复:]
引用 11 楼 beenz 的回复:
或者只改下面也OK的

JScript codefunction chunk(array,process,context){var items= 用你的方法 我只弹出一次 aaa 应该是4次~·
[/Quote]
12楼的那样.
MuBeiBei 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 beenz 的回复:]
或者只改下面也OK的

JScript codefunction chunk(array,process,context){var items= Array.prototype.concat.call(array);
setTimeout(function(){var ite= items.shift();
process.call(context,ite);if(items.length>0){
setTimeout(arguments.callee,100);
}
},100);
};
[/Quote]

用你的方法 我只弹出一次 aaa 应该是4次~·
s_liangchao1s 2009-09-25
  • 打赏
  • 举报
回复

// 不好意思 我看错题
<script type="text/javascript">
<!--
var $A = function(coll){
if(coll.item){
var l = coll.length, array = new Array(l);
while (l--) array[l] = coll[l];
return array;
}
}
window.onload = function(){
var oInputs = document.getElementsByTagName('input');
alert($A(oInputs))
};

//-->
</script>
<input type="button" id="testx" value="aa" />
<input type="button" id="testy" value="bb" />
<input type="button" id="testz" value="cc" />
<input type="button" id="testa" value="dd" />

BeenZ 2009-09-25
  • 打赏
  • 举报
回复
或者只改下面也OK的


function chunk(array,process,context){
var items = Array.prototype.concat.call(array);
setTimeout(function(){
var ite = items.shift();
process.call(context,ite);

if(items.length > 0){
setTimeout(arguments.callee,100);
}
},100);
};
BeenZ 2009-09-25
  • 打赏
  • 举报
回复
注意我代码中间

alert(oInputs instanceof Array )
alert(array instanceof Array )

证明 确实不是个array对象

后面我用了array来构造,可以用了,LZ看看
BeenZ 2009-09-25
  • 打赏
  • 举报
回复
这样

<script>
function process(){
alert('aaa');
};
function bind(grg){
Array.prototype.Function.call(grg);
}
window.onload = function(){
var oInputs = document.getElementsByTagName('input');
var array = ['a','b'];
alert(oInputs instanceof Array )
alert(array instanceof Array )
// chunk(oInputs,process); //这块传array数组就可以,传oInputs就不行报错!

oInputs.chunk=(function(process,context){
var items = Array.prototype.concat.call(this);
setTimeout(function(){
var ite = items.shift();
process.call(context,ite);

if(items.length > 0){
setTimeout(arguments.callee,100);
}
},100);
})(process)
};

</script>
<input><input>
Trinx 2009-09-25
  • 打赏
  • 举报
回复
在JS中getElementsByTagName()获得的是一个类似于数组的NodeList对象,但除了有个length属性和下标取值以外再也没有别的数组方法了,因为他不是一个真正的数组对象。
MuBeiBei 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hookee 的回复:]
要么将集合中的元素,加入到一个Array()中,然后再对Array进行操作.
[/Quote]


var array = [];
var oInputs = document.getElementsByTagName('input');
for(var i = 0; i < oInputs.length; i++){
array.push(oInputs[i]);
}
chunk(array,process);

没有别的方法了吗?


Array.prototype.slice.call(oInputs)
//这个方法为什么在IE下不支持呢?能解决吗?
hookee 2009-09-25
  • 打赏
  • 举报
回复
要么将集合中的元素,加入到一个Array()中,然后再对Array进行操作.
MuBeiBei 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hookee 的回复:]
准确的说 getElementsByTagName返回的节点列表类型 NodeList对象.
[/Quote]

那应该怎么解决呢?
我总觉得getElementsByTagName方法返回的是一个伪数组.
MuBeiBei 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 s_liangchao1s 的回复:]
JScript code
Array.prototype.slice.call(oInputs)
[/Quote]

IE下用这方法报错 说缺少JScript对象
FF下通过 没错
hookee 2009-09-25
  • 打赏
  • 举报
回复
准确的说 getElementsByTagName返回的节点列表类型 NodeList对象.
MuBeiBei 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 s_liangchao1s 的回复:]
JScript code
Array.prototype.slice.call(oInputs)
[/Quote]

报错 说缺少JScript对象
s_liangchao1s 2009-09-25
  • 打赏
  • 举报
回复

Array.prototype.slice.call(oInputs)

87,907

社区成员

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

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