JS之获取样式的简单实现方法(推荐)

基本代码

rush:js;"> <Meta charset="UTF-8"> Document

1.通过使用element.style属性获取

rush:js;">

element.style属性只能获取行内样式,不能获取

This is div

6.getPropertyValue获取CSsstyleDeclaration对象中的指定属性

rush:js;">

getPropertyValue(propertyName);中的propertyName不能是驼峰式表示

obj.currentStyle['margin-left'] 有效

obj.currentStyle['marginLeft'] 有效

window.getComputedStyle(obj,null)['margin-left'] 有效

window.getComputedStyle(obj,null)['marginLeft'] 有效

window.getComputedStyle(obj,null).getPropertyValue('margin-left') 有效

window.getComputedStyle(obj,null).getPropertyValue('marginLeft') 无效

obj.currentStyle.width 有效

obj.currentStyle.background-color 无效

obj.currentStyle.backgroundColor 有效

window.getComputedStyle(obj,null).width 有效

window.getComputedStyle(obj,null).background-color 无效

window.getComputedStyle(obj,null).backgroundColor 有效

综上,就是带有"-"的属性不能直接点出来,所以有getPropertyValue方法来处理,但是可以用[]来取代getPropertyValue

7.defaultview

在许多在线的演示代码中,getComputedStyle 是通过 document.defaultview 对象来调用的。 大部分情况下,这是不需要的, 因为可以直接通过window对象调用。但有一种情况,你必需要使用 defaultview,那是在firefox3.6上访问子框架内的样式 (iframe)

以上这篇JS之获取样式的简单实现方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...