HTMLPanel 中的 Javascript

标签 javascript html gwt panel

我想在 HTMLPanel 元素中包含 Javascript 代码,但它不起作用。请你帮助我好吗?提前致谢。

Scripts/pro.js

alert('hello');

WITH HTMLPANEL 不工作(不显示警报)

MyPage.java(它是 MyPage.html 的入口点)

String preHtml="<script type=\"text/javascript\" src=\"Scripts/pro.js\"></script>";
HTMLPanel prePanel=new HTMLPanel(preHtml);
RootPanel.get("scriptContainer").add(prePanel);

MyPage.html

<td align="left" valign="top" id="scriptContainer"></td>

没有 HTMLPANEL 也能工作(显示警告)

MyPage.html

<td align="left" valign="top"><script type="text/javascript" src="Scripts/pro.js"></script></td>

最佳答案

我认为应该反过来。引用 javadoc 的 HTMLPanel

A panel that contains HTML, and which can attach child widgets to identified elements within that HTML.

应该包装你的容器而不是你的脚本。除非您绝对有理由使用 HTMLPanel 来包装脚本,否则下面的示例有效。

HTMLPanel html = new HTMLPanel("<table><tr><td id='scriptContainer'></td></tr></table>");
RootPanel.get().add(html);  
Element script = DOM.createElement("script");
DOM.setElementAttribute(script,"language","JavaScript");
DOM.setElementAttribute(script,"src","Scripts/pro.js");
DOM.appendChild(DOM.getElementById("scriptContainer"),script);

关于HTMLPanel 中的 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7057312/

相关文章:

javascript - 为 iFrame 文档添加打印按钮

css - 如何在整个页面中居中对齐特定字符?

javascript - Google 图表 HTML 从右到左问题

java - 如何在 TextInputCell 中放置掩码?

javascript - 尝试显示 Web api 服务数据时出现空白页面

javascript - 当用户单击提交按钮时需要发布并关闭窗口

javascript - 将数组传递给 ng-click

javascript - 在更改下拉菜单后将文本放置在 td 中

javascript - 为打印而打开的选项卡永久显示 "loading"图标

java - GWT 中按顺序排列更多空格字符