骨干JS:如何删除多余的标签的看法? [英] Backbone js: How to remove extra tag in view?
问题描述
我有以下的模板:
<div class="row">
<div></div>
....
</div>
和以下观点:
var TestView = Backbone.View.extend({
tagName: "div",
template: $("#tests_template"),
initialize: function () {
_.bindAll(this, 'clickbtn');
},
events:
{
"click .btn": "clickbtn"
},
render: function () {
....
{
});
问题是,它产生以下输出:
The problem is, it produces the following output:
<div><div class="row">...</div></div>
我如何摆脱外层div的?我试图从视图中删除标记名属性,但它仍然把一个div?
How do I get rid of the outer div? I tried removing the tagName property from the view but it still puts a div?
推荐答案
更改模板摆脱外层div的:
Change your template to get rid of the outer div:
<div></div>
....
然后,告诉视图与类名创建DIV:
Then, tell the view to create the div with a class name:
tagName: "div",
className: "row"
或如果你想保持目前的模板,然后告诉视图报
来使用它(假设它已经存在于某个地方的页):
OR if you want to keep the current template, then tell the View which el
to use (assuming it exists already some place on your page):
var testView = new TestView({el: $(".row")});
修改您问,如果你能在初始化做到这一点?当然,但你需要确保您挂钩事件:
EDIT You asked if you can do this in the initializer? Sure, but you'd need to make sure that you hook the events:
initialize: function () {
this.el = $(".row");
this.delegateEvents();
_.bindAll(this, 'clickbtn');
}
老实说,虽然,前两个选项都比较脱钩IMO。
Honestly, though, the first two options are more de-coupled IMO.
这篇关于骨干JS:如何删除多余的标签的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!