所谓类数组对象,通俗的说就是具有数组的某些特性,使用上非常类似于数组。
比如使用getElementsByTagName()方法获取的dom元素集合,下面就介绍一下如何利用jQuery将其转换为数组。
代码实例如下:
<!DOCTYPE html>程序代码如下:
<script type="text/javascript">
function test(){
alert(arguments)
arguments = Array.prototype.slice.call(arguments);
alert(arguments instanceof Array);
alert(arguments)
}
test(1,2,3,4);
//※※※※※※※※※※※测试2※※※※※※※※※※※※※※
var htmlcollection = document.getElementsByTagName("h3");
alert(htmlcollection)
try{
htmlcollection = Array.prototype.slice.call(htmlcollection);
alert(htmlcollection instanceof Array);
alert(htmlcollection);
}catch(e){
alert(e)
}
//※※※※※※※※※※※测试3※※※※※※※※※※※※※※
var sheets = document.styleSheets;
alert(sheets)
try{
sheets = Array.prototype.slice.call(sheets);
alert(sheets instanceof Array);
alert(sheets);
}catch(e){
alert(e)
}
alert(arrayLike)
arrayLike = Array.prototype.slice.call(arrayLike);
alert(arrayLike instanceof Array);
alert(arrayLike);
</script>
本回答被网友采纳