Element.getStyle()不返回GWT中的任何值

时间:2012-05-13 14:55:48

标签: java html css gwt

下面我的CSS文件为样式“myButton”设置宽度“50px”,因此正确创建了宽度为50px的元素。

问题是从Java(GWT)管理元素。以下几行:

MyFile.java

final Element box1 = DOM.getElementById("myButton");
String test=box1.getStyle().getWidth();
box1.getStyle().setWidth(100, Unit.PX);
String test2=box1.getStyle().getWidth();

结果 test =“” test2 =“100px”,元素正确调整为100px。 为什么测试没有任何价值?

谢谢,

MyFile.html

<div style="width: 100%"> 
  <a  id ="myButton" href="#" class="myButton" style="text-decoration: none">Hey</a>    
</div>

MyFile.css

.myButton {
    display:block;
    padding: 10px;
    font-family:"Garamond 3 W01","Franklin Gothic Medium", "Arial Narrow", Arial, sans-serif;/*"Lucida Sans", "Lucida Sans Regular", "Lucida Grande", "Lucida Sans Unicode", Geneva, Verdana, sans-serif;*/
    font-size: medium;
    color: #000000;
    text-align: center;
    border-radius: 8px;
    background-color: #E7E7E7;
    background-image: url('bar1.png');
    border-bottom: 1px solid #BBB;
    border-top: 1px solid #BBB;
    box-shadow: 0 0 6px 0 #B3B2B7;
    text-shadow: 0 1px 0 white;
    width: 50px; 
    height: 30px;
    margin: 10px 10px 10px 10px;
}

1 个答案:

答案 0 :(得分:2)

Element#getStyle()返回与JavaScript element.style相同的内容,即将内嵌应用于元素的样式( via style=""属性)。

GWT不会模仿getComputedStyle或类似的;如果你确实需要它们(这不太可能;你应该重构你的代码),那么你将不得不使用JSNI。

请参阅https://developer.mozilla.org/en/DOM/element.style