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 相关文章推荐
Discuz! 6.1_jQuery兼容问题
Sep 23 Javascript
javascript 解析后的xml对象的读取方法细解
Jul 25 Javascript
js关闭当前页面(窗口)的几种方式总结
Mar 05 Javascript
jQuery学习笔记之jQuery.extend(),jQuery.fn.extend()分析
Jun 09 Javascript
使用window.prompt()实现弹出用户输入的对话框
Apr 13 Javascript
Atitit.js的键盘按键事件捆绑and事件调度
Apr 01 Javascript
Angular页面间切换及传值的4种方法
Nov 04 Javascript
vue图片加载与显示默认图片实例代码
Mar 16 Javascript
react-native 封装选择弹出框示例(试用ios&android)
Jul 11 Javascript
原生js FileReader对象实现图片上传本地预览效果
Mar 27 Javascript
VUE 使用中踩过的坑
Feb 08 Javascript
js事件on动态绑定数据,绑定多个事件的方法
Sep 15 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
php设计模式 Singleton(单例模式)
2011/06/26 PHP
解析php扩展php_curl.dll不加载的解决方法
2013/06/26 PHP
PHP实现的折半查找算法示例
2017/12/19 PHP
php实现表单提交上传文件功能
2018/05/28 PHP
在Javascript中为String对象添加trim,ltrim,rtrim方法
2006/09/22 Javascript
javascript动态判断html元素并执行不同的操作
2014/06/16 Javascript
jQuery带箭头提示框tooltips插件集锦
2014/11/17 Javascript
充分发挥Node.js程序性能的一些方法介绍
2015/06/23 Javascript
jquery实现的Banner广告收缩效果代码
2015/09/02 Javascript
JS Ajax请求如何防止重复提交
2016/06/13 Javascript
Express+Nodejs 下的登录拦截实现代码
2017/07/01 NodeJs
React Native仿美团下拉菜单的实例代码
2017/08/08 Javascript
vue-cli 打包后提交到线上出现 "Uncaught SyntaxError:Unexpected token" 报错
2018/11/06 Javascript
[33:23]Secret vs Serenity 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python类定义的讲解
2013/11/01 Python
web.py获取上传文件名的正确方法
2014/08/26 Python
Python基于pygame实现的font游戏字体(附源码)
2015/11/11 Python
Python使用matplotlib绘图无法显示中文问题的解决方法
2018/03/14 Python
Python实现获取nginx服务器ip及流量统计信息功能示例
2018/05/18 Python
Sanic框架应用部署方法详解
2018/07/18 Python
python对视频画框标记后保存的方法
2018/12/07 Python
python函数装饰器之带参数的函数和带参数的装饰器用法示例
2019/11/06 Python
利用CSS的Sass预处理器(框架)来制作居中效果
2016/03/10 HTML / CSS
前后端结合实现amazeUI分页效果
2020/08/21 HTML / CSS
KIKO MILANO荷兰网上商店:意大利专业化妆品品牌
2017/05/12 全球购物
美国著名的家居用品购物网站:Bed Bath & Beyond
2018/01/05 全球购物
Allen Edmonds官方网站:一家美国优质男士鞋类及配饰制造商
2019/03/12 全球购物
一份报关员的职业规划范文
2014/01/08 职场文书
小孩百日宴答谢词
2014/01/15 职场文书
入职担保书怎么写
2014/05/12 职场文书
中学教师师德承诺书
2014/05/23 职场文书
食堂采购员岗位职责
2015/04/03 职场文书
2016年班主任新年寄语
2015/08/18 职场文书
社会实践心得体会范文
2016/01/14 职场文书
CocosCreator如何实现划过的位置显示纹理
2021/04/14 Javascript
Python可视化动图组件ipyvizzu绘制惊艳的可视化动图
2022/04/21 Python