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>

转载于:https://www.cnblogs.com/jsxyz/p/10213671.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值