javascript中使用css需要注意的地方小结


Posted in Javascript onSeptember 01, 2010

1.在改变单个元素样式时,注意style对象的语法和css中使用的语法几乎是一一对应的。不过包含连字符的属性则被替换为一种“camel castring”的形式,例如:font-size现在成了fontSize,而margin-top变成了marginTop;
2.在使用“float”时,因为“float”是javascript的一个保留字,所以就不能使用style.float,而改成了style.cssFloat(IE使用的是style.styleFloat);
3. 获得元素的计算样式:
在W3C DOM下可以:

var heading = document.getElementById("heading"); 
var computedStyle = document.defaultView.getComputedStyle(heading,null); 
var computedFontFamily = computedStyle.fontFamily;//sans-serif

IE不支持使用DOM标准方法,可以:
var heading = document.getElementById("heading"); 
var computedFontFamily = heading.currentStyle.fontFamily;//sans-serif

综合上述这些方法,可以创建一个跨浏览器函数来实现
function retrieveComputedStyle(element,styleProperty){ 
var computedStyle = null; 
if(typeof element.currentStyle != "undefined"){ 
computedStyle = element.currentStyle; 
}else{ 
computedStyle = document.defaultView.getComputedStyle(element,null); 
} 
return computedStyle[styleProperty]; 
}

对照表

CSS Properties To JavaScript Reference Conversion

CSS Property JavaScript Reference
background background
background-attachment backgroundAttachment
background-color backgroundColor
background-image backgroundImage
background-position backgroundPosition
background-repeat backgroundRepeat
border border
border-bottom borderBottom
border-bottom-color borderBottomColor
border-bottom-style borderBottomStyle
border-bottom-width borderBottomWidth
border-color borderColor
border-left borderLeft
border-left-color borderLeftColor
border-left-style borderLeftStyle
border-left-width borderLeftWidth
border-right borderRight
border-right-color borderRightColor
border-right-style borderRightStyle
border-right-width borderRightWidth
border-style borderStyle
border-top borderTop
border-top-color borderTopColor
border-top-style borderTopStyle
border-top-width borderTopWidth
border-width borderWidth
clear clear
clip clip
color color
cursor cursor
display display
filter filter
font font
font-family fontFamily
font-size fontSize
font-variant fontVariant
font-weight fontWeight
height height
left left
letter-spacing letterSpacing
line-height lineHeight
list-style listStyle
list-style-image listStyleImage
list-style-position listStylePosition
list-style-type listStyleType
margin margin
margin-bottom marginBottom
margin-left marginLeft
margin-right marginRight
margin-top marginTop
overflow overflow
padding padding
padding-bottom paddingBottom
padding-left paddingLeft
padding-right paddingRight
padding-top paddingTop
page-break-after pageBreakAfter
page-break-before pageBreakBefore
position position
float styleFloat
text-align textAlign
text-decoration textDecoration
text-decoration: blink textDecorationBlink
text-decoration: line-through textDecorationLineThrough
text-decoration: none textDecorationNone
text-decoration: overline textDecorationOverline
text-decoration: underline textDecorationUnderline
text-indent textIndent
text-transform textTransform
top top
vertical-align verticalAlign
visibility visibility
width width
z-index zIndex

Usage

Internet Explorer

document.all.div_id.style.JS_property_reference = "new_CSS_property_value";

Older Netscape's (4.7 and earlier)

document.div_id.JS_property_reference = "new_CSS_property_value";

Netscape 6.0+ and Opera (and other Mozilla)

document.getElementById(div_id).style.JS_property_reference = "new_CSS_property_value";

Note the use of parentheses instead of square brackets in newer Mozilla's "getElementById()" reference.

Javascript 相关文章推荐
一段好玩的JavaScript代码
Dec 01 Javascript
可以用来调试JavaScript错误的解决方案
Aug 07 Javascript
javascript instanceof 内部机制探析
Oct 15 Javascript
Jquery之美中不足小结
Feb 16 Javascript
js模仿html5 placeholder适应于不支持的浏览器
Jan 13 Javascript
iframe的父子窗口之间的对象相互调用基本用法
Sep 03 Javascript
JQuery boxy插件在IE中边角图片不显示问题的解决
May 20 Javascript
原生js实现addClass,removeClass,hasClass方法
Apr 27 Javascript
Bootstrap 3.x打印预览背景色与文字显示异常的解决
Nov 06 Javascript
js中async函数结合promise的小案例浅析
Apr 14 Javascript
JS实现点击下拉列表文本框中出现对应的网址,点击跳转按钮实现跳转
Nov 25 Javascript
解决Vue 刷新页面导航显示高亮位置不对问题
Dec 25 Javascript
js截取函数(indexOf,join等)
Sep 01 #Javascript
qTip 基于JQuery的Tooltip插件[兼容性好]
Sep 01 #Javascript
jQuery选中select控件 无法设置selected的解决方法
Sep 01 #Javascript
JavaScript的类型转换(字符转数字 数字转字符)
Aug 30 #Javascript
De facto standard 世界上不可思议的事实标准
Aug 29 #Javascript
js 中 document.createEvent的用法
Aug 29 #Javascript
JQuery浮动DIV提示信息并自动隐藏的代码
Aug 29 #Javascript
You might like
外媒评选出10支2020年最受欢迎的Dota2战队
2021/03/05 DOTA
destoon之一键登录设置
2014/06/21 PHP
CodeIgniter错误mysql_connect(): No such file or directory解决方法
2014/09/06 PHP
PHP 魔术变量和魔术函数详解
2015/02/25 PHP
php生成图片验证码
2015/06/09 PHP
PHP的Yii框架中行为的定义与绑定方法讲解
2016/03/18 PHP
跨浏览器开发经验总结(四) 怎么写入剪贴板
2010/05/13 Javascript
js setTimeout opener的用法示例详解
2013/10/23 Javascript
JQUERY 设置SELECT选中项代码
2014/02/07 Javascript
javascript制作坦克大战全纪录(1)
2014/11/27 Javascript
JavaScript Math.floor方法(对数值向下取整)
2015/01/09 Javascript
Javascript设计模式之观察者模式的多个实现版本实例
2015/03/03 Javascript
JavaScript的Polymer框架中dom-repeat与VM的相关操作
2015/07/29 Javascript
最简单的JavaScript图片轮播代码(两种方法)
2015/12/18 Javascript
bootstrap基础知识学习笔记
2016/11/02 Javascript
微信小程序中单位rpx和rem的使用
2016/12/06 Javascript
nginx+vue.js实现前后端分离的示例代码
2018/02/12 Javascript
vue-cli3.0 特性解读
2018/04/22 Javascript
微信小程序项目实践之九宫格实现及item跳转功能
2018/07/19 Javascript
详解Vue开发微信H5微信分享签名失败问题解决方案
2018/08/09 Javascript
微信小程序 scroll-view的使用案例代码详解
2020/06/11 Javascript
微信小程序自定义modal弹窗组件的方法详解
2020/12/20 Javascript
pymongo实现控制mongodb中数字字段做加法的方法
2015/03/26 Python
在Django的模板中使用认证数据的方法
2015/07/23 Python
详解MySQL数据类型int(M)中M的含义
2016/11/20 Python
Python实现二分查找与bisect模块详解
2017/01/13 Python
简单谈谈Python中的几种常见的数据类型
2017/02/10 Python
python字符串过滤性能比较5种方法
2017/06/22 Python
浅谈Python中的bs4基础
2018/10/21 Python
对pandas通过索引提取dataframe的行方法详解
2019/02/01 Python
Django Python 获取请求头信息Content-Range的方法
2019/08/06 Python
详解Python Opencv和PIL读取图像文件的差别
2019/12/27 Python
Pycharm自动添加文件头注释和函数注释参数的方法
2020/10/23 Python
推荐信怎么写
2014/05/09 职场文书
2015年教导处教学工作总结
2015/07/22 职场文书
css3 选择器
2022/05/11 HTML / CSS