JavaScript基础---获取元素的属性(title,style,width)

<div id="box" class="box" title="abc" style="width:100px;height:100px;">123</div>
<script>
var oBox=document.getElementById('box');

//Attribute
console.log(oBox.getAttribute("title"))
console.log(oBox.getAttribute("style")) //width:100px;height:100px;字符串
oBox.setAttribute('name',"haha")
console.log(oBox.getAttribute("name"))
oBox.removeAttribute('name')

//直接oBox.name
console.log(oBox.title)
console.log(oBox.style.width)

//getComputedStyle().width / currentStyle.width 只能获取style属性里面的内容
if(typeof window.getComputedStyle==="function"){
console.log('1'+getComputedStyle(oBox).width)
}else{
console.log('2'+oBox.currentStyle.width)
}

</script>

猜你喜欢

转载自www.cnblogs.com/jsxyz/p/10213671.html