2011-02-11 72 views
1

我使用jQuery UI Autocomplete插件內的jQuery模板,以便更好地控制自動填充結果的呈現方式。我想要做的是突出顯示結果中的輸入。 http://jqueryui.com/demos/autocomplete/combobox.html是我試圖實現的一個很好的例子,只是我想用jQuery模板來實現它。在jQuery模板中調用的函數中包含標記?

這是我迄今(有一些自動完成代碼的抽象出來):

<script> 
    // in this example, this.term == 'dog' 
    $('#tmplProject').tmpl({text: "All About Dogs"}, 
    {term: this.term, highlight: function(text) { 
     return text.replace(new RegExp(
        "(?![^&;]+;)(?!<[^<>]*)(" + 
        $.ui.autocomplete.escapeRegex(this.term) + 
        ")(?![^<>]*>)(?![^&;]+;)", "gi" 
       ), "<strong>$1</strong>"); 
    }})) 
</script> 
<script id="tmplProject" type="text/x-jquery-tmpl"> 
    some other markup goes here. ${$item.highlight(name)} 
</script> 

的問題是,在<strong>元素越來越逃出來的,All about <strong>Dog</strong>s是顯示在瀏覽器了。但這種做法很有意義,因爲模板應該是標記的位置,而不是高亮方法。那麼如何將突出顯示方法的邏輯與生成的標記分開?

回答

2

{{html}}標籤是否適合您?

<script id="tmplProject" type="text/x-jquery-tmpl"> 
    some other markup goes here. {{html $item.highlight(name) }} 
</script> 

像這樣:http://jsfiddle.net/rniemeyer/baY3k/

+0

謝謝,我一定是錯過了這個地方的文檔。這對我的例子完美的作品,但我很好奇,我將如何保持模板標記,而不是突出方法本身? – carpeliam 2011-02-11 18:14:25

相關問題