网络编程
位置:首页>> 网络编程>> JavaScript>> JavaScript如何获取一个元素的样式信息

JavaScript如何获取一个元素的样式信息

作者:城里的乡下人  发布时间:2023-08-28 12:16:17 

标签:javascript,获取,元素,样式,信息

跨浏览器方法


function getStyle(elem, cssprop, cssprop2){
if(elem.currentStyle){
 return elem.currentStyle[cssprop]
 }else if(document.defaultView && document.defaultView.getComputedStyle){
 return document.defaultView.getComputedStyle(elem, "").getPropertyValue(cssprop2)
}else{
 return null
}
}

<style>
#elem{font-size:23px;}
</style>
<input id ="elem" style="font-size:18px;"/>
<script>
elem.style.fontSize = '12px';
console.log(getStyle(elem, "fontSize", "font-size"))
// 输出12px
</script>

getStyle取值的优先级是:

1.动态设置的

2.内联设置的

3.样式表设置的

4.浏览器默认设置的

来源:https://www.cnblogs.com/crazyWang/p/9936136.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com