目前,我正在學習Backbone.js的,還是在最開始,但我得到的錯誤:Backbone.js的EL錯誤
Uncaught TypeError: Cannot call method 'html' of undefined
上線此$ el.html(模板) ;
<body>
<div id="search_container"></div>
<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/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">
$(document).ready(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);
}
});
var search_view = new SearchView({ el: $("#search_container") });
});
</script>
</body>
爲什麼這個錯誤出現?
嘗試在所有腳本之前將div放在身體標記的頂部。我認爲你的JavaScript在div存在之前正在執行。 –
沒有相同的錯誤:( – Noor
你真的應該使用最新版本的骨幹,0.3.3是從石器時代。 –