[英]how can i pass a parameters to a function to get current Style
美好的一天,我想知道如何在IE中获取currentStyle,并将参数传递给这样的函数参数:
function test(el,value){
return document.getElementById(el).currentStyle[value];
}
如果我要使用类似的功能从Firefox,Chrome等获取样式,则可能会导致这种情况。
使用这样的功能:
function test(el,value){
return getComputedStyle(document.getElementById(obj))[value];
}
,其中value是元素属性,例如backgroundColor,即:
alert(test('ObjectId','backgroundColor'));
....它将在FF,Chrome ..中返回backgroundColor,但在Internet Explorer中不返回
有什么可能的解决方案..?
Thnx ..
请我不是在寻找jQuery解决方案...
这是我用来检索样式属性值的内容
function getStyle(el,sProp,toInt){
var elem = el;
if (elem.currentStyle) {
return toInt
? parseInt(elem.currentStyle[sProp],10)
: elem.currentStyle[sProp] || 0;
} else if (window.getComputedStyle) {
var compStyle = window.getComputedStyle(elem, null)[sProp];
return toInt ? parseInt(compStyle,10) : compStyle || 0;
}
return String(elem.style[sProp]||0);
}
这(非常)复杂。
我已经编写了与浏览器无关的解析器,但无法与您共享。
除非您要编写自己的框架,否则我必须问,为什么要能够解决所有问题? 是否需要特定的属性(或某些属性)? 因为那可能容易得多。
如果只需要背景颜色,那么.style.backgroundColor可能就足够了。
此外,示例脚本中还有一个错误:
alert(test('ObjectId'),'backgroundColor');
应该:
alert(test('ObjectId','backgroundColor'));
我不是第一次犯同样的错误;)-花了我半天的时间找到它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.