HTMLCollection和NodeList

2018-11-01  本文已影响0人  kenny_bai

//HTMLCollection获取

//document.getElementsByTagName('标签名'); //得到的是一个类数组HTMLCollection,没有pop(), push()或join()的方法

//HTML DOM节点列表(NodeList)

var myNodeList = document.querySelectorAll('p');

console.log('myNodeList:',myNodeList);

//HTMLCollection

var collList = document.getElementsByTagName('p');

console.log('collList:',collList);

//获取所有节点(HTMLCollection)

var allList = document.getElementsByTagName('*');

console.log('所有节点:',allList);

//HTMLDocument还有一个方法,根据标签name值获取所有对象(NodeList),主要是用在单选按钮选定上

var getNameList = document.getElementsByName('testname');

console.log('name节点:',getNameList);

//HTMLCollection与NodeList差别

//不同点

// HTMLCollection是HTML元素的集合

// NodeList是文档节点的集合

//HTMLCollection可以使用索引,name,id获取 ,可以使用namedItem方法根据标签name属性值获取对象

//NodeList只能通过索引值来获取元素

上一篇 下一篇

猜你喜欢

热点阅读