[英]how can i pass a parameters to a function to get current Style
Good day, I wonder how to get currentStyle in IE, passing parameters to an function argument like this: 美好的一天,我想知道如何在IE中获取currentStyle,并将参数传递给这样的函数参数:
function test(el,value){
return document.getElementById(el).currentStyle[value];
}
if i'd use a similar function to get Style from Firefox, Chrome and so on, it would result. 如果我要使用类似的功能从Firefox,Chrome等获取样式,则可能会导致这种情况。
using a function like this: 使用这样的功能:
function test(el,value){
return getComputedStyle(document.getElementById(obj))[value];
}
, where value is the element property like backgroundColor, ie: ,其中value是元素属性,例如backgroundColor,即:
alert(test('ObjectId','backgroundColor'));
.... it would return backgroundColor in FF, Chrome.. but not in Internet Explorer ....它将在FF,Chrome ..中返回backgroundColor,但在Internet Explorer中不返回
What r possibles solutions..? 有什么可能的解决方案..?
Thnx.. Thnx ..
please i'm not looking for a jQuery soluction... 请我不是在寻找jQuery解决方案...
Here what I use to retrieve a style property value 这是我用来检索样式属性值的内容
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);
}
This is (sadly) very complex. 这(非常)复杂。
I have written a browser independent resolver but I can't share it with you. 我已经编写了与浏览器无关的解析器,但无法与您共享。
Unless you are writing your own framework I have to ask, why do you want to be able to resolve everything? 除非您要编写自己的框架,否则我必须问,为什么要能够解决所有问题? Is there a specific property (or some properties) you want?
是否需要特定的属性(或某些属性)? Because that could be a lot easier.
因为那可能容易得多。
If you just want the background color then .style.backgroundColor is probably sufficient. 如果只需要背景颜色,那么.style.backgroundColor可能就足够了。
Also, there is a bug in your example script: 此外,示例脚本中还有一个错误:
alert(test('ObjectId'),'backgroundColor');
Should be: 应该:
alert(test('ObjectId','backgroundColor'));
Not the first time I made the same mistake ;) - took me half a day to find it 我不是第一次犯同样的错误;)-花了我半天的时间找到它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.