2013-02-19 50 views
0

我見過很多類似問題的答案,但還沒有找到我的問題的答案。 有一個html頁面。從gwt調用javascript函數。 HTMLPane

<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"; 
} 

和定義一個函數,但沒有任何幫助。 請幫助查找錯誤,或者說這是不可能的

+0

這將有助於讓我們知道您正在使用SmartClient的smartGWT。我花了一段時間才找到對其HTMLPane的引用[http://www.smartclient.com/docs/8.3/a/b/c/go.html#class..HTMLPane]。你知道_does_工作嗎?你可以調用頁面體中的隱藏功能嗎?當你的gwt代碼設置內容URL時,你看到它加載頁面嗎?無論調用'someMethod'之前的行是否執行? – 2013-02-19 18:26:09

回答

0

hide()window —成員與$wnd在調用本地方法代替$doc,即:

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

如果硬要將其連接到document,保持本地方法不變,但更正功能分配:

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

問題是,H當使用ContentsType.PAGE時,TMLPane使用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(); 
}-*/; 

Set an ID to an iframe in a HtmlPane
Calling javascript function in iframe

使用過於籠統的名稱,如「隱藏/文本」可能導致與其他腳本/對象的衝突,導致奇怪的行爲。