js 获取 html元素的样式有三种方式:style、getComputedStyle 和 currentStyle等

本文主要是介绍js 获取 html元素的样式有三种方式:style、getComputedStyle 和 currentStyle等,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



js 获取 html元素的样式有三种方式:style、getComputedStyle 和 currentStyle等。区别在于:

(1)style 只能获取行间样式,但能设置样式。

(2)getComputedStyle 和 currentStyle 能够获取 行间样式/非行间样式/浏览器默认样式,但存在浏览器兼容问题,且不能设置样式。

一、element.style 获取行间样式,以及设置样式

  1. <!DOCTYPE HTML>  
  2. <html lang="zh-cn">  
  3. <head>  
  4. <meta charset="utf-8" />  
  5. <title>Javascript</title>  
  6. <style>  
  7. *{margin: 0;padding: 0;}  
  8. #box{width: 100px;height: 100px;margin-left: 100px;}  
  9. </style>  
  10. </head>  
  11. <body>  
  12. <div id="box" style="background-color:#ccc;margin-top:100px;"></div>  
  13. <script>  
  14. window.onload = function(){   
  15.     var oBox = document.getElementById('box');  
  16.         console.log(oBox.style.width);          //结果为:100px  
  17.     console.log(oBox.style.background); //结果:rgb(204,204,204),但ie下为空  
  18.     console.log(oBox.style.backgroundColor); //结果:rgb(204,204,204)或#ccc  
  19.     console.log(oBox.style.margin);     //结果为空  
  20.     console.log(oBox.style.marginTop);  //结果:100px  
  21.     oBox.style.height = '120px';        //设置样式  
  22. }  
  23. </script>  
  24. </body>  
  25. </html>  
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<title>Javascript</title>
<style>
*{margin: 0;padding: 0;}
#box{width: 100px;height: 100px;margin-left: 100px;}
</style>
</head>
<body>
<div id="box" style="background-color:#ccc;margin-top:100px;"></div>
<script>
window.onload = function(){	var oBox = document.getElementById('box');console.log(oBox.style.width);          //结果为:100pxconsole.log(oBox.style.background);	//结果:rgb(204,204,204),但ie下为空console.log(oBox.style.backgroundColor); //结果:rgb(204,204,204)或#cccconsole.log(oBox.style.margin);		//结果为空console.log(oBox.style.marginTop);	//结果:100pxoBox.style.height = '120px';		//设置样式
}
</script>
</body>
</html>

style总结:

 

(1)对于复合属性(如background),假设行间设置了样式:background-color:#333,不能通过 element.style.background 来获取(见上面例子)。

(2)css属性使用驼峰法,如 backgroundColor、marginTop等。

(3)不同浏览器,一些 css 属性值可能会发生转换,如例子中的 background-color,标准浏览器会转换为 rgb 形式。

二、getComputedStyle 获取css属性值

 

  1. <!DOCTYPE HTML>  
  2. <html lang="zh-cn">  
  3. <head>  
  4. <meta charset="utf-8" />  
  5. <title>Javascript</title>  
  6. <style>  
  7. #box{width: 100px;height: 100px;margin-left: 100px;}  
  8. </style>  
  9. </head>  
  10. <body>  
  11. <div id="box" style="background-color:#ccc;margin-top:100px;"></div>  
  12. <script>  
  13. window.onload = function(){   
  14.     var oBox = document.getElementById('box');  
  15.     var a = getComputedStyle(oBox, null)['width']; // 100px  
  16.     var b = getComputedStyle(oBox, null).getPropertyValue('backgroundColor'); //chrome为null, ie为空  
  17.     var c = getComputedStyle(oBox, null)['backgroundColor'];// rgb(204,204,204)  
  18.     var d = getComputedStyle(oBox,null)['padding'];// chrome为0px, ie为空  
  19.     console.log(a, b, c, d);  
  20. }  
  21. </script>  
  22. </body>  
  23. </html>  
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<title>Javascript</title>
<style>
#box{width: 100px;height: 100px;margin-left: 100px;}
</style>
</head>
<body>
<div id="box" style="background-color:#ccc;margin-top:100px;"></div>
<script>
window.onload = function(){	var oBox = document.getElementById('box');var a = getComputedStyle(oBox, null)['width']; // 100pxvar b = getComputedStyle(oBox, null).getPropertyValue('backgroundColor'); //chrome为null, ie为空var c = getComputedStyle(oBox, null)['backgroundColor'];// rgb(204,204,204)var d = getComputedStyle(oBox,null)['padding'];// chrome为0px, ie为空console.log(a, b, c, d);
}
</script>
</body>
</html>

 

getComputedStyle总结:

(1)标准浏览器,ie9+以上支持 getComputedStyle。

(2)对于复合属性:使用 getPropertyValue 获取属性值时,不能使用驼峰写法,如:例子中的 getpropertyValue('backgroundColor') 无法正确获得值,而必须写成 background-color。

(3)另外,以下写法也正确:getComputedStyle(oBox, null)['backgroundColor']、getComputedStyle(oBox, null)['background-color'], 以及 getComputedStyle(oBox, null).backgroundColor 等。

(4)当没有设置某个属性值时,chrome 会读取浏览器该属性的默认值,而 ie9+ 下结果为空。如例子中的 padding。

(5)getComputedStyle 第二个参数为”伪类“,一般用不着,设置为 null 即可。

三、IE 下 currentStyle 获取css 属性值

还是上面的例子:

 

[javascript] view plain copy print?

  1. <script>  
  2. window.onload = function(){   
  3.     var oBox = document.getElementById('box');  
  4.     var a = oBox.currentStyle['width']; // 100px  
  5.     var b = oBox.currentStyle['background-color'];  // #ccc  
  6.     var c = oBox.currentStyle['backgroundColor'];   // #ccc  
  7.     var d = oBox.currentStyle.backgroundColor;     // #ccc  
  8.     //var e = oBox.currentStyle.background-color;  错误  
  9.     var e = oBox.currentStyle['padding'];       // 0px  
  10.     console.log(a, b, c, d, e);  
  11. }  
  12. </script>  
<script>
window.onload = function(){	var oBox = document.getElementById('box');var a = oBox.currentStyle['width']; // 100pxvar b = oBox.currentStyle['background-color'];	// #cccvar c = oBox.currentStyle['backgroundColor'];	// #cccvar d = oBox.currentStyle.backgroundColor;	   // #ccc//var e = oBox.currentStyle.background-color;  错误var e = oBox.currentStyle['padding'];		// 0pxconsole.log(a, b, c, d, e);
}
</script>

 

currentStyle 总结:

(1)只在 IE 下支持(网上 opera 也支持,但未测)。

(2)注意 ['backgroundColor']、['background-color'] 和 .backgroundColor 等写法。

(3)未设置的属性值,currentStyle 会读取浏览器默认值,如例子中的 padding。

四、不同浏览器下,获取 css 属性值 的兼容写法

 

[javascript] view plain copy print?

  1. function getStyle(oElement, sName){  
  2.     return oElement.currentStyle ? oElement.currentStyle[sName] : getComputedStyle(oElement, null)[sName];  
  3. }  

 

这篇关于js 获取 html元素的样式有三种方式:style、getComputedStyle 和 currentStyle等的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1012730

相关文章

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.