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

相关文章

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob