2013-03-20 73 views
0

我有一个CSS类中声明:以gwt获取css类属性值?

gwt-Label { 
    font-size: 16px; 
} 

有没有一种方法,我可以查询字体大小在运行时的价值?喜欢的东西:

public void foo() { 
    CssFoo css = new CssFoo("gwt-Label"); 
    float fontSize = css.getAttribute("font-size"); 
    println("Your font size is: " + fontSize); 
} 

感谢

回答

0

你需要一块JSNI代码来获取computedValue,或一个更好的选择是使用gwtquery

import static com.google.gwt.query.client.GQuery.*; 
... 

// the second parameter set to true means: return the real computed value 
double size = $(my_widget).cur("font-size", true); 

如果你有兴趣与JS,你可以做这样的事情做(但应在现代浏览器中工作)

private static final double getPropertyValue(Element elem, String prop) /*-{ 
    return $wnd.getComputedStyle(elem,null).getPropertyValue(prop); 
}-*/ 

但我会gquery与所有浏览器去,做更多像隐藏的元素,camelized属性等的解决方法。

0

不,你不能。

Style class构造函数是GWT中的隐形所以你不能创建一个Object。

但是你可以前

得到一个元素的样式对象:

TextBox textbox= new TextBox(); 
Style style = textbox.getElement().getStyle(); 
String fontSize = style.getFontSize(); 

String attribute = textbox.getElement().getAttribute("fontSize");//camelcase 

String styleAttribute = DOM.getStyleAttribute(textbox.getElement(), "fontSize"); 
+1

只是一个说明:'getStyleAttribute'不返回[计算值](https://developer.mozilla.org/en-US/docs/CSS/computed_value)(可以在CSS表单中设置的值,或者初始值),但通过js或'style ='...''属性将值设置为属性。 – 2013-03-21 12:28:47

+0

谢谢非常多@Manolo.I没有意识到这一点。 – 2013-03-21 15:09:52

0

我最近做,它在GWT中解决了简单地与;

fontSize = ComputedStyle .getStyleProperty(this.getElement(),“fontSize”);

这将得到您的类以前提供给它的计算值。