gpt4 book ai didi

backbone.js - Backbone View 的父元素?

转载 作者:行者123 更新时间:2023-12-02 01:46:43 26 4
gpt4 key购买 nike

如何为 Backbone View 设置父元素?

var TodosView = Backbone.View.extend({
tagName: 'p', // required, but defaults to 'div' if not set
className: 'container', // optional, you can assign multiple classes to
// this property like so: 'container homepage'
id: 'todos', // optional

initialize: function(){

// debugger
this.$el.html("bamboo4a")
$("body").append(this.$el);
}
});
var todosView = new TodosView();

我不想写 $("body").append

最佳答案

对于主视图,您可以在创建 View 对象时通过将选项传递给其构造函数(即选项 el)来设置 View 元素。

var MyView = Backbone.View.extend({
template: '<p>Hello World!</p>',
render: function() {
this.$el.html(this.template);
}
});

new MyView({
el: 'body' // or el: '#content' and so on
}).render();

Documentation

Demo

Demo with more detailed example of the application

关于backbone.js - Backbone View 的父元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25219970/

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