var p2 = document.getElementById("test").offsetParent;
// 获取整个html页面
var x = document.getElementsByTagName("p");
注意
HTMLCollection 不是一个数组!
HTMLCollection 看起来可能是一个数组,但其实不是。
你可以像数组一样,使用索引来获取元素。
// HTMLCollection 无法使用数组的方法: valueOf(), pop(), push(), 或 join() 。
属性 / 方法 描述
item() 返回 HTMLCollection 中指定索引的元素。
length 返回 HTMLCollection 中元素的数量。
namedItem() 返回 HTMLCollection 中指定 ID 或 name 属性的元素。
类数组转换数组
return Array.prototype.slice.call(obj,0); 转换nodelist数组为真正数组
你可以通过父节点获得childNodes(n),如果要获得节点下的所有a元素。可以遍历childNodes所得数组,然后判断targetName
属性的值
网友评论