下面是我编写的代码,它是来自互联网的教程示例(https://cdnjs.com/libraries/backbone.js/tutorials/what-is-a-view/)
<<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>
</head>
<body>
<div id ="search_container">a</div>
<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">
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);
},
events:{
"click input[type=button]": "doSearch"
},
doSearch: function(){
alert("search for " + $('#search_input').val());
}
});
var search_view = new SearchView({el: $('#search_container')});
</script>
</body>
</html>
我不明白我做错了什么,请指导我。感谢
答案 0 :(得分:1)
nikoshr是对的,你必须使用带小写的扩展和形式制作你必须改变的例子
var template = _.template($('search_template').html(),{});
到
var template = _.template($('#search_template').html(),{});