我有一个 GWT 应用程序需要从 java 代码 (GWT) 设置 html 字段 我试过了,但元素总是空的
Element element = Document.get().getElementById("elementID");
if (element != null)
{
GWT.log("found element with id:" + "elementID");
// some code....
}
项目结构:
|---Main.html:加载编译模块的页面
|---page.html:需要从GWT更新的页面
所以我的问题是,是否可以从 GWT 代码设置 page.html 中的字段,如果可以的话怎么办?
Note: page.html is show after clicking anchor defined like this:Anchor anchor = new Anchor( "message", false, "page.html", "_blank");
编辑: 页面.html:
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
</head>
<body style="font-size:62.5%;">
<div align="center" width="100%" height="80%">
<table height="80%" width="40%">
<tr valign="bottom">
<td align="center">
<div id="progressbar"></div>
</td>
<td><a id="cancel" href="#"><img src="close_icon_x.png" height="32px" width="32px"/></a></td>
</tr>
<tr><td valign="top"><div id ="elementID"></div>
</td><td></td></tr>
</table>
</div>
</body>
</html>
主.html:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" language="javascript" src="project/MyProject.nocache.js"></script>
<link media="all" type="text/css"
href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/base/jquery-ui.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"
type="text/javascript" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"
type="text/javascript" charset="utf-8"></script>
<link type="text/css" rel="stylesheet" href="Style.css">
</head>
-->
<body>
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled
in order for this application to display correctly.
</div>
</noscript>
</body>
</html>
最佳答案
我认为解决方案类似于这篇文章中的解决方案: GWT: DOM.getElementById doesn't work!
确实 Riley 是正确的,您在哪里定义 elementId ?
您可以设置字段。问题的解决方案是为什么你会得到一个空指针。
更新:您确定实际的 page.html 代码在调用 getElementID 之前已经呈现,否则您将始终得到 Null。
关于java - 从 GWT 设置 html 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7338034/