笔记-[1]-获取元素样式值的兼容性问题解决方法。
2014-06-03 23:12
507 查看
在获取某些元素的样式值时需要用到不同的兼容方法来获取,如获取div的height的值
代码:
<html>
<style>
#div1{width:100px;height:100px;background:red;}//获取非行间样式
</style>
<div id="div1">
</div>
</html>
JS兼容版获取样式值:
<script type="text/javascript">
window.onload=function(){
//定义一个function getStyle
var oDiv=document.getElementById('div1');
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];//ie下
}else{
return getComputedStyle(obj,false)[attr];//非ie下
}
}
//调用方法获取值
getStyle(oDiv,'width');//100px
}
</script>
/*主要的兼容代码*/
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];//ie下
}else{
return getComputedStyle(obj,false)[attr];//非ie下
}
}
或者
return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj,false)[attr];
代码:
<html>
<style>
#div1{width:100px;height:100px;background:red;}//获取非行间样式
</style>
<div id="div1">
</div>
</html>
JS兼容版获取样式值:
<script type="text/javascript">
window.onload=function(){
//定义一个function getStyle
var oDiv=document.getElementById('div1');
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];//ie下
}else{
return getComputedStyle(obj,false)[attr];//非ie下
}
}
//调用方法获取值
getStyle(oDiv,'width');//100px
}
</script>
/*主要的兼容代码*/
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];//ie下
}else{
return getComputedStyle(obj,false)[attr];//非ie下
}
}
或者
return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj,false)[attr];
相关文章推荐
- JS获取元素属性、样式getComputedStyle()和currentStyle方法兼容性问题
- js无法获取style样式的问题解析与解决方法
- JavaScript(获取或设置html元素的宽,高,坐标),确定和判断鼠标是否在元素内部,二级导航菜单鼠标离开样式问题解决
- 解决不同浏览器下获取样式的兼容性问题
- 笔记-[js兼容]-滚动条的滚动距离的兼容性问题解决方法。
- .net 2.0 中对于非服务器端的input元素 type为file后台无法获取的问题的解决方法
- (转)用js无法获取style样式的问题解析与解决方法
- WPF学习笔记 - ”指定的元素已经是另一个元素的逻辑子元素。请先将其断开连接。“问题的解决方法之一
- 在从池中获取连接之前超时时间已过,所有池连接都已被使用并已达到最大池大小的问题解决方法
- 解决使用attachEvent函数时,this指向被绑定的元素的问题的方法
- struts学习笔记--解决了struts action中的execute方法不执行的问题
- .net获取EXCEL列数据为空的问题解决方法
- CSS兼容性解决方法(盒模型)- !important的IE7,Firefox问题
- 1&amp;gt;s.cpp(465) : error C2448: “main”: 函数样式初始值设定项类似函数定义 问题的解决方法
- CSS兼容性解决方法(盒模型)- !important的IE7,Firefox问题 最终解决方法
- AS3.0 加载XML#1088 文档中根元素后面的标记格式必须正确,问题解决方法
- css 行级元素在多浏览器下的宽度问题 与解决方法
- Optimizer与Debugger兼容性问题的解决方法
- 笔记:解决文件互相包含问题的小方法
- word使用样式常碰到的问题和解决方法参考