Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将参数传递给函数以获得当前样式_Javascript_Cross Browser - Fatal编程技术网

Javascript 如何将参数传递给函数以获得当前样式

Javascript 如何将参数传递给函数以获得当前样式,javascript,cross-browser,Javascript,Cross Browser,你好, 我想知道如何在IE中获得currentStyle,将参数传递给函数参数,如下所示: function test(el,value){ return document.getElementById(el).currentStyle[value]; } function test(el,value){ return getComputedStyle(document.getElementById(obj))[value]; } 如果我使用一个类似的函数从Firefox、Chrom

你好, 我想知道如何在IE中获得currentStyle,将参数传递给函数参数,如下所示:

function test(el,value){
  return document.getElementById(el).currentStyle[value];
}
function test(el,value){
  return getComputedStyle(document.getElementById(obj))[value];
}
如果我使用一个类似的函数从Firefox、Chrome等浏览器中获取样式,结果会是

使用如下函数:

function test(el,value){
  return document.getElementById(el).currentStyle[value];
}
function test(el,value){
  return getComputedStyle(document.getElementById(obj))[value];
}
,其中value是元素属性,如backgroundColor,即:

alert(test('ObjectId','backgroundColor'));
。。。。 它将返回背景色FF,Chrome。。但不是在Internet Explorer中

什么是可能的解决方案

Thnx

请注意,我不是在寻找jQuery解决方案……

这(很遗憾)非常复杂

我已经编写了一个独立于浏览器的解析器,但无法与您共享

除非您正在编写自己的框架,否则我不得不问,为什么您希望能够解决所有问题? 是否有您想要的特定属性(或某些属性)?因为那会容易得多

如果您只需要背景色,那么.style.backgroundColor可能就足够了

此外,示例脚本中还有一个bug:

alert(test('ObjectId'),'backgroundColor');
应该是:

alert(test('ObjectId','backgroundColor'));

这不是我第一次犯同样的错误;)-我花了半天的时间找到它,这里是我用来检索样式属性值的

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);
}

使用currentStyle的脚本片段应该在IE中工作。它不工作吗?为什么不使用jquery?如果他们已经为你回答了问题,请查看!目标是在调用函数之前检索未知属性,但我在这里编写的函数中的问题是,Internet explorer将参数“value”解析为未知属性,即在我传递参数之前,它解析currentStyle[value]。当meta-content http equiv=“x-ua-compatibility”设置为“IE=EmulateIE8”时,就会发生这种情况,因此,通过这种方式,您可以自己尝试。顺便说一下,我正在写我自己的框架。非常感谢。