0

同様の質問に対する回答をたくさん見てきましたが、私の質問に対する回答は見つかりませんでした。HTMLページがあります。

<body>
  <div id="text">some text</div>
  <script>
     function hide()
     {
         document.getElementById("text").style.display = "none";
     }
  </script>
</body>

gwtのコード

HTMLPane panel = new HTMLPane();
panel.setContentsType(ContentsType.PAGE);
panel.setContentsURL("pages/index.html");

public native void someMethod(HTMLPane panel)/*-{
    $doc.hide();
}-*/;

しかし、何も機能しません。関数を次のように定義しようとしました

document hide = function hideF()
{
    document.getElementById("text").style.display = "none";
}

さまざまな位置で関数を定義しますが、何も役に立ちませんでした。エラーを見つけるのを手伝ってください、またはそれは不可能であると言ってください

4

2 に答える 2

0

hide()のメンバーです—呼び出し元のネイティブ メソッドを次のように置き換えwindowます。$doc$wnd

public native void someMethod(HTMLPane panel)/*-{
    $wnd.hide();
}-*/;

どうしても にアタッチする場合はdocument、ネイティブ メソッドを変更せずに、関数の割り当てを修正します。

document.hide = function()
{
    document.getElementById("text").style.display = "none";
}
于 2013-02-21T09:12:41.827 に答える
0

問題は、 ContentsType.PAGEが使用されている場合、HTMLPane が iframe を使用することです。
というわけで、hide()iframeの子ウィンドウの機能です。
を使用する必要がある場合はContentsType.PAGE、次のように動作します。

HTMLPane panel = new HTMLPane();
panel.setContentsType(ContentsType.PAGE);
panel.setContents("<iframe id='" + "id_internal_panel_1" + "' src='" + "pages/index.html" + "' style='border:none'/>");

// above use of iframe instead of using the one created by HTMLPane, could cause styling and other issues

// following did not work for me
// panel.setContentsURL("pages/index.html");
// panel.getElement().setId("id_internal_panel_1");
// panel.getElement().setPropertyString("name", "id_internal_panel_1");
// panel.getElement().setPropertyString("id", "id_internal_panel_1");

IButton button = new IButton("Hide");
button.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent clickEvent) {
        someMethod();
    }
});

public native void someMethod()/*-{
    $doc.getElementById("id_internal_panel_1").contentWindow.hide();
    // $wnd.document.getElementById("id_internal_panel_1").contentWindow.hide();

    // can use following with panel.setContentsURL("pages/index.html");, if ContentsType is not set to ContentsType.PAGE
    // $wnd.hide();
}-*/;

HtmlPane の iframe に ID を設定する iframe で
javascript 関数を呼び出す

「hide/text」のようなあまりにも一般的な名前を使用すると、他のスクリプト/オブジェクトと競合し、奇妙な動作を引き起こす可能性があります。

于 2013-04-27T23:52:42.960 に答える