如何在GWT中设置属性?
我没有从 DOM.setElementAttribute 中得到任何效果,我做错了什么吗?
class MyListBox extends com.google.gwt.user.client.ui.ListBox {
....
protected void setHoverAutoWidth() {
addDomHandler(new MouseOverHandler() {
public void onMouseOver(MouseOverEvent event) {
DOM.setElementAttribute(getElement(), "width", "auto");
}
}, MouseOverEvent.getType());
addDomHandler(new BlurHandler(){
public void onBlur(BlurEvent event) {
DOM.setElementAttribute(getElement(), "width", "100px");
}
}, BlurEvent.getType());
}
}
(我知道更改宽度的方法比直接设置样式属性要少一些,但我现在不关心 css。)
编辑: 糟糕,刚刚意识到宽度不会改变样式宽度,只是向标签添加一个宽度属性(这解释了为什么什么也没有发生)。仍然欢迎任何有关如何修改样式的建议!
I'm not getting any effect out of DOM.setElementAttribute
, am I doing something wrong?
class MyListBox extends com.google.gwt.user.client.ui.ListBox {
....
protected void setHoverAutoWidth() {
addDomHandler(new MouseOverHandler() {
public void onMouseOver(MouseOverEvent event) {
DOM.setElementAttribute(getElement(), "width", "auto");
}
}, MouseOverEvent.getType());
addDomHandler(new BlurHandler(){
public void onBlur(BlurEvent event) {
DOM.setElementAttribute(getElement(), "width", "100px");
}
}, BlurEvent.getType());
}
}
(I know there are less hacky ways to change the width than to set the style attribute directly, but I don't care about css right now.)
Edit: Oops, just realized that width does not change the style width, just adds a width attribute to the tag (which explains why nothing happens). Any suggestions on how to modify the style are still welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我很确定它将尝试设置元素的
width
属性(对于您正在处理的元素类型可能不存在),不是 CSS <代码>宽度属性。您可能正在寻找
setStyleAttribute
。I'm pretty sure that will attempt to set the
width
attribute of the element (which may not exist for the type of element you are working on), NOT the CSSwidth
property.You're probably looking for
setStyleAttribute
.使用 DOM.setStyleAttribute
Use DOM.setStyleAttribute
在 GWT 的更高版本中,您可以使用
getElement().getStyle().setWidth(100, Unit.PX)
,与中基于字符串的方法相比,它为您提供了一些额外的编译时正确性。 DOM 类。使用getElement().getStyle().clearWidth()
将宽度设置回auto
。In later versions of GWT you can use
getElement().getStyle().setWidth(100, Unit.PX)
, which gives you a bit of extra compile-time correctness over the String-based methods in the DOM class. UsegetElement().getStyle().clearWidth()
to set the width back toauto
.