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 相关文章推荐
dojo 之基础篇(二)之从服务器读取数据
Mar 24 Javascript
不要在cookie中使用特殊字符的原因分析
Jul 13 Javascript
IE6下出现JavaScript未结束的字符串常量错误的解决方法
Nov 21 Javascript
javascript history对象(历史记录)使用方法(实现浏览器前进后退)
Jan 07 Javascript
js调试工具Console命令详解
Oct 21 Javascript
JS实现点击网页判断是否安装app并打开否则跳转app store
Nov 18 Javascript
详解vue-resource promise兼容性问题
Jun 20 Javascript
jQuery修改DOM结构_动力节点Java学院整理
Jul 05 jQuery
vue实现密码显示隐藏切换功能
Feb 23 Javascript
Bootstrap Table实现定时刷新数据的方法
Aug 13 Javascript
vue路由--网站导航功能详解
Mar 29 Javascript
关于vue中如何监听数组变化
Apr 28 Vue.js
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
Smarty安装配置方法
2008/04/10 PHP
php+ajax做仿百度搜索下拉自动提示框(有实例)
2012/08/21 PHP
PHP中array_map与array_column之间的关系分析
2014/08/19 PHP
聊聊 PHP 8 新特性 Attributes
2020/08/19 PHP
找到了一篇jQuery与Prototype并存的冲突的解决方法
2007/08/29 Javascript
js 可拖动列表实现代码
2011/12/13 Javascript
用jQuery实现的智能隐藏、滑动效果的返回顶部代码
2014/03/18 Javascript
微信小程序 devtool隐藏的秘密
2017/01/21 Javascript
轻松实现jQuery添加删除按钮Click事件
2017/03/13 Javascript
利用Mongoose让JSON数据直接插入或更新到MongoDB
2017/05/03 Javascript
BootStrap 表单控件之单选按钮水平排列
2017/05/23 Javascript
详解bootstrap导航栏.nav与.navbar区别
2017/11/23 Javascript
Angularjs过滤器实现动态搜索与排序功能示例
2017/12/13 Javascript
JS运动特效之同时运动实现方法分析
2018/01/24 Javascript
微信网页登录逻辑与实现方法
2019/04/29 Javascript
使用vue for时为什么要key【推荐】
2019/07/11 Javascript
一看就会的vuex实现登录验证(附案例)
2020/01/09 Javascript
Vue组件间的通信pubsub-js实现步骤解析
2020/03/11 Javascript
在Python中使用swapCase()方法转换大小写的教程
2015/05/20 Python
python获取一组数据里最大值max函数用法实例
2015/05/26 Python
玩转python爬虫之URLError异常处理
2016/02/17 Python
python subprocess 杀掉全部派生的子进程方法
2017/01/16 Python
Python的iOS自动化打包实例代码
2018/11/22 Python
Jacobi迭代算法的Python实现详解
2019/06/29 Python
Python程序暂停的正常处理方法
2019/11/07 Python
python3利用Axes3D库画3D模型图
2020/03/25 Python
Django中的AutoField字段使用
2020/05/18 Python
HTML5+CSS3网页加载进度条的实现,下载进度条的代码实例
2016/12/30 HTML / CSS
英国精品买手店:Browns Fashion
2016/09/29 全球购物
孕妇装中的著名品牌:Isabella Oliver(伊莎贝拉·奥利弗)
2016/10/31 全球购物
印度尼西亚最大的电商平台:Tokopedia(印尼版淘宝)
2017/12/02 全球购物
Topshop美国官网:英国快速时尚品牌
2019/05/16 全球购物
英国鞋网:Rubber Sole
2020/03/03 全球购物
教师应聘自荐信范文
2014/03/14 职场文书
奠基仪式策划方案
2014/05/15 职场文书
民族学专业大学生职业规划范文:清晰未来的构想
2014/09/20 职场文书