我正在使用backbone.js、underscore.js 和jQuery。我从http://jsfiddle.net/thomas/C9wew/6/获取了以下示例代码:
<!DOCTYPE html>
<html>
<head>
<title>backbone.js</title>
</head>
<body>
<div id="search_container">
Container
</div>
<script src="/js/jquery-1.7.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/js/underscore-min.js" type="text/javascript" charset="utf-8"></script>
<script src="/js/backbone-min.js" type="text/javascript" charset="utf-8"></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'>
//<![CDATA[
$(window).load(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);
this.el.innerHTML = 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>
</body>
</html>
我希望“el”是一个 jQuery 包装元素,因为是通过 jQuery 选择器 $("#search_container") 选择的,但不是因为它没有像 html() 这样的函数。
如果我在 "el : $("#search_container")" 行设置断点,则 "el" 元素具有函数 html(),这是正常行为。当调用渲染函数时,“el”元素不再具有 html() 函数..任何想法?