gpt4 book ai didi

javascript - View 渲染 - backbone.js

转载 作者:行者123 更新时间:2023-11-30 07:25:59 24 4
gpt4 key购买 nike

如何在 backbone.js result wanted 中生成此结果 <p><h3>fgfdgdfg</h3></p>

var TodoView = Backbone.View.extend({
el:'p',
render: function () {
$('body').append(this.$el.html("<h3>fgfdgdfg</h3>"));
}
});

var todoView = new TodoView();
todoView.render();

最佳答案

使用 tagName而不是 el .
编辑以修复错误的 html,thanks @muistooshort .刚刚删除了 <p>完全。

var TodoView = Backbone.View.extend({

tagName:'h3',
render: function () {
$('body').append(this.$el.html("fgfdgdfg"));
}

});

var todoView = new TodoView();
todoView.render();

你设置el如果存在您希望 View 使用的现有 DOM 元素。设置tagName告诉 Backbone 为 View 的根生成一个“h3”元素。

您也可以这样做(我更喜欢这种方式;避免设置“el”):

var TodoView = Backbone.View.extend({

tagName:'h3',
render: function () {
this.$el.html("fgfdgdfg");
return this;
}

});

// view is more reusable now, since it doesn't have the 'body' part in render.
// So, another instance could be rendered and added to the page somewhere else.
var todoView = new TodoView();
$('body').append(todoView.render().el);
var todoView2 = new TodoView();
$('body').append(todoView2.render().el);

如果您的 html 已经包含要用于 View 的“h3”元素,您可以这样做:

// assuming this html on the page already:
// <body><h3></h3></body>
var TodoView = Backbone.View.extend({

// setting 'el' tells backbone to use the existing <h3>.
// You probably would want to use an id rather than 'h3' though.
el:'h3',
render: function () {
this.$el.html("fgfdgdfg");
return this;
}

});

var todoView = new TodoView();
todoView.render();

关于javascript - View 渲染 - backbone.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15073752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com