ボタンをクリックしてliを既存のulに追加し、他のボタンをクリックして1つずつ削除するjqueryがあります。作成は機能していますが、削除は機能していません。追加と削除を使用して、li を追加および削除しました。
これが私のコードです:
$(document).ready(function(){
var ListView = Backbone.View.extend({
el: $('body'), // el attaches to existing element
events: {
'click button#add': 'addItem',
'click button#remove': 'removeItem'
},
initialize: function(){
_.bindAll(this, 'render', 'addItem','removeItem');
this.counter = 0; // total number of items added thus far
this.render();
},
render: function(){
$(this.el).append("<button id='add'>Add list item</button>");
$(this.el).append("<button id='remove'>Remove list item</button>");
$(this.el).append("<ul></ul>");
},
addItem: function(){
this.counter++;
$('ul', this.el).append("<li>hello world"+this.counter+"</li>");
},
removeItem: function(){
alert(this.counter);
$('li',this.el).remove("<li>hello world"+this.counter+"</li>");
}
});
var listView = new ListView();
});