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