使用Javascript跨浏览器(IE8-)getComputedStyle?

use*_*156 23 javascript internet-explorer styles cross-browser

由于IE8不支持getComputedStyle,我们只能使用currentStyle.但是,它不会返回某些属性的实际"计算"值.

例如:

<style type="text/css">
#div {/* no properties are defined here */}
</style>
Run Code Online (Sandbox Code Playgroud)
<div id="div">div</div>
Run Code Online (Sandbox Code Playgroud)
// returns "medium" instead of 0px
document.getElementById('div').currentStyle.borderLeftWidth

// returns "auto" instead of 0px
document.getElementById('div').currentStyle.marginLeft

// returns "undefined" instead of 1
document.getElementById('div').currentStyle.opacity
Run Code Online (Sandbox Code Playgroud)

有没有人在不使用jQuery或其他Javascript库的情况下为所有属性提供跨浏览器解决方案?

小智 16

这是一个跨浏览器函数来获取计算样式...

getStyle = function (el, prop) {
    if (typeof getComputedStyle !== 'undefined') {
        return getComputedStyle(el, null).getPropertyValue(prop);
    } else {
        return el.currentStyle[prop];
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以将其作为实用程序存储在对象中,或者只是按提供的方式使用它.这是一个示例演示!

// Create paragraph element and append some text to it
var p = document.createElement('p');
p.appendChild(document.createTextNode('something for fun'));

// Append element to the body
document.getElementsByTagName('body')[0].appendChild(p);

// Set hex color to this element
p.style.color = '#999';

// alert element's color using getStyle function
alert(getStyle(p, 'color'));
Run Code Online (Sandbox Code Playgroud)

检查此演示以查看它的实际效果:

getStyle = function(el, prop) {
  if (getComputedStyle !== 'undefined') {
    return getComputedStyle(el, null).getPropertyValue(prop);
  } else {
    return el.currentStyle[prop];
  }
}

// Create paragraph element and append some text to it
var p = document.createElement('p');
p.appendChild(document.createTextNode('something for fun'));

// Append element to the body
document.getElementsByTagName('body')[0].appendChild(p);

// Set hex color to this element
p.style.color = '#999';

// alert element's color using getStyle function
console.log(getStyle(p, 'color'));
Run Code Online (Sandbox Code Playgroud)
p {
  color: red;
}
Run Code Online (Sandbox Code Playgroud)


小智 7

你不想使用jquery,但没有什么可以防止你偷看代码并看看他们如何处理它:-)

在jquery代码里面有一个关于这个评论的引用,似乎是重点(另请阅读整篇文章).这是应该处理你的问题的jquery代码:

else if ( document.documentElement.currentStyle ) {
    curCSS = function( elem, name ) {
        var left, rsLeft,
            ret = elem.currentStyle && elem.currentStyle[ name ],
        style = elem.style;

    // Avoid setting ret to empty string here
    // so we don't default to auto
    if ( ret == null && style && style[ name ] ) {
        ret = style[ name ];
    }

    // From the awesome hack by Dean Edwards
    // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291

    // If we're not dealing with a regular pixel number
    // but a number that has a weird ending, we need to convert it to pixels
    // but not position css attributes, as those are proportional to the parent element instead
    // and we can't measure the parent instead because it might trigger a "stacking dolls" problem
    if ( rnumnonpx.test( ret ) && !rposition.test( name ) ) {

        // Remember the original values
        left = style.left;
        rsLeft = elem.runtimeStyle && elem.runtimeStyle.left;

        // Put in the new values to get a computed value out
        if ( rsLeft ) {
            elem.runtimeStyle.left = elem.currentStyle.left;
        }
        style.left = name === "fontSize" ? "1em" : ret;
        ret = style.pixelLeft + "px";

        // Revert the changed values
        style.left = left;
        if ( rsLeft ) {
            elem.runtimeStyle.left = rsLeft;
        }
    }

    return ret === "" ? "auto" : ret;
};
}
Run Code Online (Sandbox Code Playgroud)


Eug*_*scu 7

代替 :

getComputedStyle !== 'undefined'
Run Code Online (Sandbox Code Playgroud)

它应该是 :

typeof getComputedStyle !== 'undefined'
Run Code Online (Sandbox Code Playgroud)

否则它永远不会奏效.