NodeList类数组对象: HTMLCollection , NamedNodeMap

nodeList类数组对象的特点
1,nodeList是一种类数组对象,用于保存一组有序的节点。
2,通过方括号来访问nodeList的值,有item方法与length属性。
3,它并不是Array的实例,没有数组对象的方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="js/domready.js"></script><!--必须导入该文件,搜狗输入domready.js-->
<script>
myReady(function(){
var div=document.getElementById('div');//获取div对象
console.log(div.childNodes);
//获得节点列表(nodeList),这是属于操作DOM第一套API,所以会计算空文本节点,
//(google chrome)显示:NodeList(5) [text, ul#ul1, text, ul#ul2, text] text表示空文本节点
//(firefox)显示:NodeList(5) [ #text, ul#ul1, #text, ul#ul2, #text ] #text表示空文本节点
//console.log(div.childNodes[0]);//通过方括号获取对象,显示:获取第一个nodeList对象 #text(空文本节点)
//console.log(div.childNodes[3]);//通过方括号获取对象,如:第三个对象 ul#ul2

var ul=document.getElementById("ul1");//获取ul对象
var nodeList=ul.childNodes;//获得ul下面的所有子节点对象列表 nodeList并不是一个数据类型,而是一个类数组对象,即多个对象的集合,但不是数组。
//alert(typeof nodeList);//返回的是 Object类型。
/**
* 把nodeList转化为数组对象 可以兼容IE5以上,兼容性很好。
* @param {object} nodeList 获取所有子元素对象列表
* @return {array} [返回数组对象]
*/
function markArray(nodeList){//把nodeList转换成数组
/*var arr=new Array();//创建新数组存放nodeList
for (var i = 0; i < nodeList.length; i++) {
arr[i]=nodeList[i];//把nodelist存放到数组中。
}
return arr;//返回数组*/


/**如果不考虑IE8及以下,这个方法是最好的
* 非常高效的把nodeList转化为数组对象 IE8以及IE8以下不兼容
* @param {object} nodeList 获取所有子节点列表对象
* @return {Array} 返回数组对象
*/
function markArray(nodeList){//可以直接把nodeList直接转换成Array数组。
return Array.prototype.slice.call(nodeList);//可以直接把nodeList直接转换成Array数组。
}

//这个方法最好,通用方法:
var arr=null;//用于存放对象的变量,一般都给赋值null。其他可以赋值为""。
try{
return Array.prototype.slice.call(nodeList);//可以直接把nodeList直接转换成Array数组。IE8以及IE8以下不兼容
}catch(e){
arr=new Array();//不要定义在不需要的作用域。
for (var i = 0; i < nodeList.length; i++) {//可以兼容IE7以上,兼容性很好。
arr.push(nodeList[i]);//等于 arr[i]=nodeList[i]
}
return arr;
}
}
var newarray=markArray(nodeList);//把nodeList转换成数组,
//console.log(markArray(nodeList));//输出数组对象:Array(7) [ #text, li#oneLi, #text, li#twoLi, #text, li#threeLi, #text ],查看是不是数组,可以在控制台点击,查看prototype(原型)这个属性:Array
newarray.push("<h1>You are my sumshime!</h1>");//添加对象,返回的是数组的长度,而不是数组本身。
console.log(newarray);//返回数组:(8) [text, li#oneLi, text, li#twoLi, text, li#threeLi, text, "<h1>You are my sumshime!</h1>"] 。h1这个节点只是被添加到newarray数组中,并没有添加到dom树中去。
})
</script>
</head>
<body id="body">
<div id="div">
<ul id="ul1">
<li id="oneLi"><a href="">1111</a><a href="" id="onea">22222</a><span id="span1"> </span> <span id="span2"></span></li>
<li id="twoLi"><a href="">4444</a><a href="" >5555</a><a href="">6666</a></li>
<li id="threeLi"><a href="">7777</a><a href="" >8888</a><a href="">9999</a></li>
</ul>
<ul id="ul2">
<li></li>
<li></li>
<li></li>
</ul>
</div>
</body>

============

HTMLCollection NamedNodeMap 两个类数组对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="js/domready.js"></script><!--必须导入该文件,搜狗输入domready.js-->
<script>
myReady(function(){
//以下是HTMLCollection类数组对象//
//var scripts=document.scripts;
//console.log(scripts);//返回:HTMLCollection(2) [script, script]
//var links=document.links;
//console.log(links);//返回:HTMLCollection(10) [a, a#onea, a, a, a, a, a, a, a, a, onea: a#onea]
//var images=document.images;
//console.log(images);//返回:HTMLCollection(2) [img, img]
//var forms=document.forms;
//console.log(forms);//返回:HTMLCollection(2) [form, form]
//var cells=document.getElementById("tr").cells;//获取指定tr下面的所有单元格
//console.log(cells);//返回:HTMLCollection(3) [td, td, td](火狐)
//var options=document.getElementById("city").options;//获取指定下拉列表下所有的选项
//console.log(options);//返回:HTMLOptionsCollection(5) [option, option, option, option, option, selectedIndex: 0]
//var ps=document.getElementsByTagName("p");//获取文档结构中所有的p标签
//console.log(ps);//返回:HTMLCollection(3) [p, p, p]

//var cells=document.getElementById("tr").cells;
//console.log(cells);//返回HTMLCollection类数组对象:HTMLCollection(3) [td#td, td, td, td: td#td](谷歌)
//console.log(cells.length);
//console.log(cells.item(2));
//console.log(cells.item(3));//超过类数组对象的长度时,显示为null
//console.log(cells.namedItem("td"));//返回 cells 类数组中先查找 id="td"的cell对象,如不存在,则查找name="td"的cell对象,如果存在多个name="td",则返回第一个。如果两个都不存在,则返回null对象。
//=================以上是HTMLCollection类数组对象======================

 //NodeList是节点的集合,HTMLCollection是元素节点的集合,NamedNodeMap是特性节点的集合(所有属性的集合attributes),它们都是类数组对象

//=================以下是NamedNodeMap类数组对象======================
// var box=document.getElementById("tab");//获取table对象
// box.setAttribute("name","two");//设置box对象的属性及属性值。
// console.log(box);//所有属性都可以在火狐浏览器中查看,chrome浏览器只显示该对象(包括子节点和孙子节点)
// var attributes=box.attributes;//获得box对象的所有属性。
// console.log(attributes);//返回的是NamedNodeMap(box属性的集合)
// console.log(attributes.length);//返回box对象属性集合中的长度
// console.log(attributes.style);//返回box对象属性集合中的样式
// console.log(attributes.item(0));//返回box对象属性集合中的第一个属性
// console.log(attributes.item(1));//返回box对象属性集合中的第二个属性
// console.log(attributes.getNamedItem("style"));//获取指定属性的值。
// console.log(attributes.removeNamedItem("style"));//删除指定属性,页面内的样式就会消失

//=================HTMLCollection类数组对象创建节点实例======================
var ps=document.getElementsByTagName("p");//获得HTMLCollection类数组对象
var length=ps.length;
alert(length);
var i=0;
while (i<length) {//这里不能写i=ps.length,(死循环),因为HTMLCollection类数组对象活的,每当文档结构发生变化时,都会进行更新。HTMLCollection,NamedNodeMap,NodeList
var a=document.createElement("p");
var txt=document.createTextNode("<h1>you are my sumshime"+i);
a.appendChild(txt);
document.getElementById("div").appendChild(a);
i++;
}

})
</script>
</head>
<body id="body">
<div id="div">
<ul id="ul1">
<li id="oneLi"><a href="">1111</a><a href="" id="onea">22222</a><span id="span1"> </span> <span id="span2"></span></li>
<li id="twoLi" name="oneLi"><a href="">4444</a><a href="" >5555</a><a href="">6666</a></li>
<li id="threeLi"><a href="">7777</a><a href="" >8888</a><a href="">9999</a></li>
</ul>
<table id="tab" style="border: 1px solid #333;background-color: #fof; display:block;">
<tr id="tr1" name="tr1">
<td id="td">table</td>
<td name="td">table1</td>
<td name="td">table2</td>
</tr>
<tr id="tr2">
<td>table</td>
<td>table</td>
<td>table</td>
</tr>
</table>
<img src="" alt="" style="display: block; border: 1px solid; width: 100px; height: 100px; background-color: #f00;"><img src="" alt="" style="display: block; border: 1px solid; width: 100px; height: 100px; background-color: #00f;">
<form action="">
姓名:<input type="text" name="name" value="张三">
<input type="button" name="submit" value="提交">
</form>
<form action="">
地址:<input type="text" name="address" value="中国">
<input type="button" name="submit" value="提交">
</form>
<a href=""></a><a href=""></a>
<select name="city" id="city">
<option value="0">北京</option>
<option value="1">上海</option>
<option value="2">深圳</option>
<option value="3">重庆</option>
<option value="4">厦门</option>
</select>
<p>基础挤出机会</p>
<p>基础挤出机会</p>
<p>基础挤出机会</p>
</div>
</body>

猜你喜欢

转载自www.cnblogs.com/Knowledge-is-infinite/p/10883699.html
今日推荐