簡體   English   中英

如何在GWTQuery中使用$(document).ready(function)?

[英]How do you use the $(document).ready(function) in GWTQuery?

我一直在使用GWTQuery編碼項目,但是找不到與$(document).ready(function).等效的GWTQuery $(document).ready(function).

我嘗試做一個:

$(new Function(){ /* Function comes here */ });

盡管這不會產生語法錯誤,但其中寫入的任何代碼均不會產生結果。

您無需編寫任何就緒函數。

鏈接的問題一樣 ,onModuleLoad()實際上與ready事件相同。 默認情況下,onModuleLoad犯規運行,直到畢竟在頁面資源已加載。

如果是INW,則可以直接開始編寫onModuleLoad

GWTQuery指南所示,我們可以開始在onModuleLoad中編寫代碼。

public void onModuleLoad() {
  //Hide the text and set the width and append an h1 element
  $("#text").hide()

}

gQuery中的$(Function)構造$(Function)具有不同的含義,在Functions使用語法$(this)是一個技巧。

在下面的示例中, $(this)$("#input")$(element)的快捷方式。 請注意, this指向內部Function

 // gwtQuery version
 $("#input").click(new Function(){public void f() {
      $(this).text('whatever');
 }});

如您所見,我們這樣做是為了擁有與jQuery非常相似的代碼,因為將代碼從jQuery移植到gQuery更容易。 在下面的情況下, this指向執行單擊的上下文: input element

 // jQuery version
 $("#input").click(function() {
      $(this).text('whatever');
 });

關於onReady問題,請參閱@Baadshah的回答和我的評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM