2012-08-30 21 views
2

好的,所以我有这个功能用于从指定元素返回所需的计算样式。它适用于我测试过的所有桌面浏览器,但它在移动Safari浏览器中不起作用。如果我将getComputedStyle回调记录到控制台,它就是一个很好的[对象CCStyleDeclaration],但是当我记录getPropertyValue调用时,它只是返回“null”而不是正确的样式。任何帮助都会很棒。谢谢!无法在iOS中使用getComputedStyle访问样式

Utils.getStyle = function(element, style){ 
    var strValue = ""; 
    if(document.defaultView && document.defaultView.getComputedStyle){ 
     strValue = document.defaultView.getComputedStyle(element, "").getPropertyValue(style); 
    }else if(element.currentStyle){ 
     style = style.replace(/\-(\w)/g, function (strMatch, p1){ 
      return p1.toUpperCase(); 
     }); 
     strValue = element.currentStyle[style]; 
    }; 
    return strValue; 
}; 
+0

您倾销/检查了document.defaultView.getComputedStyle(element,“”)'的内容吗? – Stecman

+0

我在桌面上,但有没有办法在iOS控制台中获取整个转储?这件事驱使我坚果,它的网络调试黑暗时代。 ;) – rcooper102

+0

是的,我知道你的意思!看看[这个SO问题](http://stackoverflow.com/questions/323517/is-there-an-equivalent-for-var-dump-php-in-javascript):将数据转储到页面上可能是最容易做的事情。 – Stecman

回答

1

在我的评论大约window.getComputedStyle VS document.defaultView.getComputedStyle有任何物质的情况下,我在自己的图书馆使用此功能:

getComputedStyle : function(element){ 
    var styles = element.currentStyle || getComputedStyle(element, null); 
    return styles; 
} 

适合您的函数签名:

Utils.getStyle = function(element, style){ 
    var styles = element.currentStyle || getComputedStyle(element, null); 

    // Prevent a 'Cannot read property X of null' error 
    if(styles != null){ 
     return styles[style]; 
    } else { 
     return null; 
    } 
} 

我不幸的是,不能在iOS中测试这个,但我很想知道它是否工作。