通过 gwt 中的 id 获取小部件
我有一堆动态生成的 TextBox-es。在创建步骤中,我为它们分配 ID 属性。 例如,
id = ...
Button b = new Button();
b.setText("add textbox");
b.addClickHandler(new Clickhandler() {
Textbox tb = new TextBox();
tb.getElement().setId(Integer.toString(id));
tb.setText("some text");
}
id += 1;
我需要稍后通过他们的 ID 访问他们,但我不能这样做。 我尝试使用 DOM 对象来获取小部件,但它产生了一个异常:
String id = "some id";
Element el = DOM.getElementById(id);
String value = el.getAttribute("value"); - this line produces an exception.
我还尝试使用 el.getInnerText、el.getNodeValue - 没有运气。我在 chrome 调试器中看到 - 文本框没有“value”属性。
I have a bunch of TextBox-es generated dynamically. At the step of creation I'm assigning the ID property for them.
e.g.
id = ...
Button b = new Button();
b.setText("add textbox");
b.addClickHandler(new Clickhandler() {
Textbox tb = new TextBox();
tb.getElement().setId(Integer.toString(id));
tb.setText("some text");
}
id += 1;
I need to access them later by their IDs, but I cannot do it.
I tried to use the DOM object in order to get a widget, but it produces an exception:
String id = "some id";
Element el = DOM.getElementById(id);
String value = el.getAttribute("value"); - this line produces an exception.
I've also tried to use el.getInnerText, el.getNodeValue - no luck. I have see in the chrome debugger - the textboxes don't have the 'value' property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过以下方式获取与元素关联的小部件:
you can get the widget associated to an element this way:
既然您正在使用 gwt java 代码构建文本框,为什么不将它们放入地图中并稍后访问它们呢?
Since you are constructing your textboxes in gwt java code, why not put them into a map and access them later?
需要记住的一件事是 HTML/DOM 中属性和属性之间的区别。在您的示例中,“值”是一种属性。您可以尝试 Element#getPropertyString。过去,属性和属性可以互换使用,但在现代浏览器中,情况已不再如此。
One thing to keep in mind is the difference between an attribute and a property in HTML/DOM. In your example, "value" is a property. You could try Element#getPropertyString. It used to be that attributes and properties were used interchangeably, but in modern browsers that's no longer the case.