2015-06-29 44 views
0

我想將此JavaScript代碼翻譯爲JSNI代碼。來自JSNI的外部函數調用不起作用

腳本導入

<script src="jquery-1.11.2.min.js"></script> 
<script src="jquery.typeahead.min.js"></script> 
<script src="autocompletetest/autocompletetest.nocache.js"></script> 

腳本

$('#q').typeahead({ 
    minLength: 1, 
    order: "asc", 
    group: true, 
    groupMaxItem: 6, 
    hint: true, 
    dropdownFilter: "All", 
    href: "https://en.wikipedia.org/?title={{display}}", 
    template: "{{display}}, <small><em>{{group}}</em></small>", 
    source: { 
     country: { 
      data: data.countries 
     }, 
     capital: { 
      data: data.capitals 
     } 
    }, 
    ... 

$doc.getElementsByClassName("q").typeahead({ 
    ... 
}) 

但我發現了這個錯誤:

@com.citi.sevi.client.AutocompleteTest::loadJquery()([]): $doc.getElementsByClassName(...).typeahead is not a function 

什麼導致這個錯誤?

+0

歡迎來到堆棧溢出!我編輯了你的問題,使代碼更具可讀性,並希望得到更多人的幫助。祝你好運! –

回答

1

jQuery的$函數不僅僅是一個getElementsByClassName包裝器,它將結果包裝在一個jQuery對象中,插件存在於該對象中。要調用typeahead方法,您需要使用$win.$("q")

+0

謝謝湯姆,只需在現有代碼中添加$ wnd即可完美實現此語法。 '$ wnd。$('#q')。typeahead({'。我在「SmartGWT」上使用了完全相同的語法,沒有顯示任何內容,'$ wnd。$('#q')。長度「本身爲0.對象沒有被識別任何想法爲什麼? – Jess

+0

使用Smart gwt實現jquery和外部js的解決方案在以下鏈接中定義[https://www.smartclient.com/smartgwt/javadoc /com/smartgwt/client/docs/DomIntegration.html] – Jess