我有一个按钮,按下按钮时我需要创建更多按钮。
这是我为按钮编写的内容:
<div class="response-container" id="response-container">
<a href="#" class="button add-button" id="add-button">Add button</a>
</div>
我正在尝试学习 Backbone.js,因此我编写了一个 View 来在单击添加按钮时创建更多按钮。
createButton = Backbone.View.extend({
events: {
'click .add-button': 'add_script'
},
add_script: function() {
console.log('Pressed');
//create a new button from here?
}
});
谁能帮我做一下吗? console.log('Pressed')
甚至不起作用。我正在尝试学习 Web 开发,请问您有什么可以建议我的文档吗?欢迎在 Backbone.js 中提出所有建议
最佳答案
您必须创建 View 的对象。 像这样
var createButton = Backbone.View.extend({
el: '#response-container',
events: {
'click .add-button': 'add_script'
},
initialize: function() {
console.log("initialized");
},
add_script: function() {
console.log('Pressed');
//create a new button from here?
this.$el.append('<input type="button" value="new button"/>');
}
});
var cb = new createButton();
这是DEMO
关于javascript - 单击按钮创建按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34568059/