getComputedStyle就像IE8的javascript函数一样

mon*_*tss 10 javascript gwt internet-explorer-8

我正在尝试在Java GWT代码中编写一个Javascript函数,它获取以下样式的值

"direction", "fontFamily", "fontSize", "fontSizeAdjust", "fontStyle", "fontWeight", "letterSpacing", "lineHeight", "padding", "textAlign", "textDecoration", "textTransform", "wordSpacing"
Run Code Online (Sandbox Code Playgroud)

getComputedStyle不支持这种功能,按照我的理解,不同的IE8浏览器的所有有用

我在这里查看了关于smiler主题的帖子,但是他们都未能获得上述样式之一

smiler主题帖1,2.

这是我没有IE8特殊情况的初始解决方案

public static native String getStyleProperty(Element element, String style) /*-{
        if (element.currentStyle) {
            return element.currentStyle[style];
        } else if (window.getComputedStyle) {
            return window.getComputedStyle(element, null).getPropertyValue(
                    style);
        }
    }-*/;
Run Code Online (Sandbox Code Playgroud)

getComputedStyle有关IE8 良好替换功能的任何建议吗?

Nie*_*els 7

请看这里:http://snipplr.com/view/13523/

代码:

if (!window.getComputedStyle) {
    window.getComputedStyle = function(el, pseudo) {
        this.el = el;
        this.getPropertyValue = function(prop) {
            var re = /(\-([a-z]){1})/g;
            if (prop == 'float') prop = 'styleFloat';
            if (re.test(prop)) {
                prop = prop.replace(re, function () {
                    return arguments[2].toUpperCase();
                });
            }
            return el.currentStyle[prop] ? el.currentStyle[prop] : null;
        }
        return this;
    }
}
Run Code Online (Sandbox Code Playgroud)

例:

window.onload = function() {
    var compStyle = window.getComputedStyle(document.getElementById('test'), "");

    alert(compStyle.getPropertyValue("color"));
    alert(compStyle.getPropertyValue("float"));
    alert(compStyle.getPropertyValue("background-color"));
}
Run Code Online (Sandbox Code Playgroud)


Mak*_*pov 5

这是解决方案。它基于此Trick,但是为了解决两个问题,我对其进行了扩展。

第一个问题是borderTopWidthleftbottomright)的el.currentStyle收益为形容词- ,,'thin' -或。技巧将返回异常。'medium''thick''none'

第二个问题-某些值将无法正确计算。如opacity和其他。您可以通过对所有属性应用此Trick方法来自己检查它:

var _style = el.currentStyle;
for (var key in _style) {
      /// here is the Trick.....
}
Run Code Online (Sandbox Code Playgroud)

最后,这是我基于假设的解决方案,即我知道我想要通过此函数获得的所有属性:

if (!window.getComputedStyle) window.getComputedStyle = function(el){
    var __style = el.currentStyle,
        _style = {};
    for (var i in __style) {
        _style[i] = __style[i]; 
    }

    // IE8 returns border widths as adjectives
    if (style.indexOf("border") === 0)
        switch (_style[style]) {
            case "thin":
                _style[style] = 2;
                break;
            case "medium":
                _style[style] = 4;
                break;
            case "thick":
                _style[style] = 6;
                break;
            default:
                _style[style] = 0;
        }

    // based on http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291        
    var leftCopy = el.style.left;
    var runtimeLeftCopy = el.runtimeStyle.left;
    // some properties, that I want to use
    _styleParams = {
        left : 1,
        right : 1,
        top : 1,
        bottom : 1,
        width : 1,
        height : 1,
        borderLeftWidth : 1,
        borderRightWidth : 1,
        borderTopWidth : 1,
        borderBottomWidth : 1,
        paddingLeft : 1,
        paddingRight : 1,
        paddingTop : 1,
        paddingBottom : 1,
        marginLeft : 1,
        marginRight : 1,
        marginTop : 1,
        marginBottom : 1
    }
    for (var key in _styleParams) {             
        el.runtimeStyle.left = el.currentStyle.left;            
        el.style.left = _style[key];                        
        _style[key] = el.style.pixelLeft;
        el.style.left = leftCopy;
        el.runtimeStyle.left = runtimeLeftCopy;             
    }


    // opacity for IE8
    if (_style.filter.match('alpha')) {
        _style.opacity = _style.filter.substr(14);
        _style.opacity = parseInt(_style.opacity.substring(0, _style.opacity.length-1)) / 100;
    } else {
        _style.opacity = 1;
    }}
Run Code Online (Sandbox Code Playgroud)


mon*_*tss 1

我使用了与原始解决方案类似的方法,并添加了一个额外的案例来处理内联样式,检查当前文档是否支持的方法也getComputedStyle有点不同,它检查的是而document.defaultView不是窗口本身,这是完整的功能

public static native String getStyleProperty(Element el, String prop) /*-{
        var computedStyle;
        if (document.defaultView && document.defaultView.getComputedStyle) { // standard (includes ie9)
            computedStyle = document.defaultView.getComputedStyle(el, null)[prop];

        } else if (el.currentStyle) { // IE older
            computedStyle = el.currentStyle[prop];

        } else { // inline style
            computedStyle = el.style[prop];
        }
        return computedStyle;

    }-*/;
Run Code Online (Sandbox Code Playgroud)

来源