【javascript】DOM操作方法(6)——Element节点的方法

(1)位置方法

getBoundingClientRect()  
// getBoundingClientRect返回一个对象,包含top,left,right,bottom,width,height // width、height 元素自身宽高
// top 元素上外边界距窗口最上面的距离
// right 元素右外边界距窗口最上面的距离
// bottom 元素下外边界距窗口最上面的距离
// left 元素左外边界距窗口最上面的距离
// width 元素自身宽(包含border,padding) 
// height 元素自身高(包含border,padding) 

getClientRects()   //返回当前元素在页面上形参的所有矩形。

// 元素在页面上的偏移量  
var rect = el.getBoundingClientRect()  
return {   
  top: rect.top + document.body.scrollTop,   
  left: rect.left + document.body.scrollLeft  
}

(2)属性方法

Element.getAttribute():读取指定属性  
Element.setAttribute():设置指定属性  
Element.hasAttribute():返回一个布尔值,表示当前元素节点是否有指定的属性  
Element.removeAttribute():移除指定属性
<script>
    function myfunction(){
        document.getElementById("demo1").setAttribute("id","demo");
        document.getElementById("demo1").getAttribute("id");
    }
</script>

(3)查找方法·

Element.querySelector()  
Element.querySelectorAll()  
Element.getElementsByTagName()  
Element.getElementsByClassName()

 

posted on 2017-10-17 11:03  忆华灯纵博  阅读(248)  评论(0编辑  收藏  举报