我正在关注主干教程http://backbonetutorials.com/what-is-a-view/并且在监听事件段落中有以下短语:
请记住,事件侦听器只能附加到“el”属性的子元素。
然后他们正在执行以下代码:
<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>
<div id="search_container"></div>
<script type="text/javascript">
SearchView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
var template = _.template( $("#search_template").html(), {} );
this.$el.html( 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>
但是 el 属性#search _container
根本没有孩子。他们正在将侦听器应用于input
哪个是search_template
我想我误解了一些东西,因为它对我没有任何意义。谁能纠正我?