我正在尝试将 ID 添加到 GWT 元素。我尝试过以下代码。
在SubscribeView.java文件中,定义了existingFundRadioButton字段,我为其设置一个ID,如下所示:
DOM.setElementAttribute( existingFundRadioButton.getElement(),
"ID",
"existingFundRadioButton" )
我尝试使用 existingFundRadioButton.getElement().setId("existingFundRadioButton")
但它不起作用。
这是我的代码。
<g:FlowPanel width="100%">
<g:FlowPanel>
<g:HTMLPanel ui:field='searchPanel' styleName="{res.fdcWidgets.box}">
<table class="{res.fdcWidgets.inputTable}">
<colgroup>
<col width="25%" />
<col width="75%" />
</colgroup>
<tbody>
<tr>
<td>
<fdc:I18NRadioButton name="fund"
ui:field="existingFundRadioButton" checked="true"
messageKey="buy.existingFundLabel" />
</td>
<td>
<hli:HoldingsListBoxWidget ui:field="holdingsListBox" />
</td>
</tr>
</tbody>
</table>
</g:HTMLPanel>
</g:FlowPanel>
<g:FlowPanel styleName="{res.fdcWidgets.contentRightColumn}">
<fund:FundInformationWidget ui:field="fundInformationWidget" />
</g:FlowPanel>
</g:FlowPanel>
最佳答案
旧解决方案
如果您陷入旧版本的 GWT - 对于 RadioButton,我们使用特定于浏览器的方法。我们通过扩展 RadioButton 来实现这一点。
MyRadioButton extends RadioButton{
public void setId( String id )
{
getElement().setId( id );
Element child = DOM.getChild( getElement(), 0 );
DOM.setElementAttribute( child, "id", id + CONST_INPUT_ELEMENT );
final Element e_label = DOM.getChild( getElement(), 1 );
// Handling IE
DOM.setElementAttribute( e_label, "htmlFor", id + CONST_LABEL_ELEMENT );
// Handling Other Browsers
DOM.setElementAttribute( e_label, "for", id + CONST_LABEL_ELEMENT );
}
}
新解决方案
对于那些幸运地拥有支持所有小部件的sureDebugId 的最新GWT 的人,您可以在从UIObject 继承的所有ui 元素中使用ensureDebugId("someid")
!!!!
关于java - 在 GWT 中设置元素 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14255315/