1

为什么我会收到错误 el is underfined -- 谢谢

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>

<script type ="text/javascript">
    var SimpleView = Backbone.View.extend({
        tagName: 'li',
        id: 'todo-id',
        className: 'todo-class',
        render: function() {
            var htmls = '<h3>jhjljlkjkljkljkljkljkjkjj</h3>';
            //this.$el.html(htmls);
            $(this.el).html(htmls);
        }



    });
    var simpleView = new SimpleView();
    simpleView.render();
    //console.log(simpleView.el);



    </script>
4

2 回答 2

3

您的代码有效。

http://jsfiddle.net/GYGF7/

var SimpleView = Backbone.View.extend({
    tagName: 'li',
    id: 'todo-id',
    className: 'todo-class',
    render: function() {
        var htmls = '<h3>jhjljlkjkljkljkljkljkjkjj</h3>';
        //this.$el.html(htmls);
        $(this.el).html(htmls);
    }



});
var simpleView = new SimpleView();
simpleView.render();
console.log(simpleView.el);

您的问题是您指向的是一个非常旧版本的骨干网(0.3.3)。

更新您的依赖链接以加载最新版本。

于 2013-02-22T16:43:21.093 回答
3

您需要定义什么el是:

   var SimpleView = Backbone.View.extend({
        el: $("YourElement"),
        tagName: 'li',
        id: 'todo-id',
        className: 'todo-class',
        render: function() {
            var htmls = '<h3>jhjljlkjkljkljkljkljkjkjj</h3>';
            //this.$el.html(htmls);
            $(this.el).html(htmls);
        }
    });

编辑

看来我可能错了:

如果指定。如果不是,el 是一个空的 div。(来自http://backbonejs.org/#View-el

于 2013-02-22T16:26:49.567 回答