getComputedStyle像IE8的javascript函数 [英] getComputedStyle like javascript function for IE8
问题描述
我试图在Java GWT代码中编写一个获得以下样式值的Javascript函数
I'm trying to write a Javascript function inside a Java GWT code that gets the value of the following styles
"direction", "fontFamily", "fontSize", "fontSizeAdjust", "fontStyle", "fontWeight", "letterSpacing", "lineHeight", "padding", "textAlign", "textDecoration", "textTransform", "wordSpacing"
The getComputedStyle
was useful in all browsers except IE8 which doesn't support such function as I understand
我在这里查看关于smiler主题的帖子,但他们都没有得到上述样式之一
I looked at the posts about smiler subject here but all of them failed to get one of the above styles
smiler subject posts 1, 2.
这里是我的初始解决方案没有IE8的特殊情况
Here is my initial solution without the IE8 special case
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);
}
}-*/;
好的 getComputedStyle
替换函数的任何建议对于IE8?
Any suggestions for a good getComputedStyle
replacement function for IE8 ?
推荐答案
我使用了类似的方法来处理内联样式,检查当前文档是否支持 getComputedStyle
有点不同,它检查 document.defaultView
而不是窗口本身,这里是完整的功能
I used a similar method to my original solution with an additional case to handle inline styles, also the way to check if the current document support the getComputedStyle
is a bit different it checks in the document.defaultView
instead of the window itself, here is the full function
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;
}-*/;
这篇关于getComputedStyle像IE8的javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!