0

我正在使用backbone.js、underscore.js 和jQuery。我从http://jsfiddle.net/thomas/C9wew/6/获取了以下示例代码:

<!DOCTYPE html>
<html>
    <head>
        <title>backbone.js</title>
    </head>
    <body>
        <div id="search_container">
            Container
        </div>

        <script src="/js/jquery-1.7.2.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="/js/underscore-min.js" type="text/javascript" charset="utf-8"></script>
        <script src="/js/backbone-min.js" type="text/javascript" charset="utf-8"></script>

        <script type="text/template" id="search_template">
            <label>Search</label>
            <input type="text" id="search_input" />
            <input type="button" id="search_button" value="Search" />
        </script>

        <script type='text/javascript'>
            //<![CDATA[
            $(window).load(function() {

                SearchView = Backbone.View.extend({
                    initialize : function() {
                        this.render();
                    },
                    render : function() {
                        // Compile the template using underscore
                        var template = _.template($("#search_template").html(), {});
                        // Load the compiled HTML into the Backbone "el"
                        //this.el.html(template);
                        this.el.innerHTML = template;
                    },
                    events : {
                        "click input[type=button]" : "doSearch"
                    },
                    doSearch : function(event) {
                        // Button clicked, you can access the element that was clicked with event.currentTarget
                        alert("Search for " + $("#search_input").val());
                    }
                });

                var search_view = new SearchView({
                    el : $("#search_container")
                });

            });
            //]]>

        </script>
    </body>
</html>

我希望“el”是一个 jQuery 包装元素,因为是通过 jQuery 选择器 $("#search_container") 选择的,但不是因为它没有像 html() 这样的函数。

如果我在 "el : $("#search_container")" 行设置断点,则 "el" 元素具有函数 html(),这是正常行为。当调用渲染函数时,“el”元素不再具有 html() 函数..任何想法?

4

1 回答 1

0

您链接的示例基于旧版本的骨干网(0.3.3)。您的示例可能使用最新版本 (0.9.2),其中this.elDOM 元素this.$el是缓存的 jQuery|Zepto 元素,与this.el.

这意味着,如果您想使用该html方法,请使用this.$el.html(template). 检查此 Fiddle http://jsfiddle.net/gFcRR/1/以获取更新的示例。

于 2012-05-15T10:11:06.877 回答