0
未定義index.html中的index.js骨幹this.el在本地
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello-backbonejs</title>
<script src="js/jquery.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<script src="js/underscore.js"></script>
<script src="js/backbone.js"></script>
<script src="js/app/index.js"></script>
</head>
<body></body>
</html>
(function($){
// **ListView class**: Our main app view.
var ListView = Backbone.View.extend({
el: $('body'), // attaches `this.el` to an existing element.
// `initialize()`: Automatically called upon instantiation. Where you make all types of bindings, _excluding_ UI events, such as clicks, etc.
initialize: function(){
_.bindAll(this, 'render'); // fixes loss of context for 'this' within methods
console.log(this);
this.render(); // not all views are self-rendering. This one is.
},
// `render()`: Function in charge of rendering the entire view in `this.el`. Needs to be manually called by the user.
render: function(){
console.log(this);
$(this.el).append("<ul> <li>hello world</li> </ul>");
}
});
// **listView instance**: Instantiate main app view.
var listView = new ListView();
})(jQuery);
,如果我訪問此通過file:/// C:/用戶在/ usr/Desktop/backbone/index.html,this.el未定義。爲什麼?
如果我複製粘貼相同的代碼在jsfiddle其工作。不知道爲什麼
你也可以替換'EL:$(「身體」)'與'el:'身體''(無論如何IMO都好得多)。 –
確實,你可以做到這一點,而且這是一個更好的方式,但這不是問題。我改變他的代碼來閱讀你描述的方式,問題依然存在 –