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>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM