backbone.js视图remove

Backbone.js的remove方法用于从DOM中删除视图。它调用stopListening删除视图具有listenTo的所有绑定事件。
句法:

view.remove()

让我们举个例子。
请参阅以下示例:
< !DOCTYPE html> < head> < title>View Remove Example< /title> < script src="http://img.readke.com/220416/0IAW162-0.jpg" type="text/javascript">< /script> < script src="http://img.readke.com/220416/0IAS429-1.jpg" type="text/javascript">< /script> < script src="http://img.readke.com/220416/0IAS5A-2.jpg" type="text/javascript">< /script> < /head> < body> < div id="mydiv">< /div> < script type="text/javascript"> var ViewDemo = Backbone.View.extend({ events: {'click button': 'removeFunc' }, removeFunc: function () {this.remove(); document.write("After removing, view becomes: ", $('#mydiv').length); }, render: function () { this.$el.html('< button>click to remove< /button>'); }, initialize:function(){this.render(); } }); var myview = new ViewDemo({el: '#mydiv'}); < /script> < /body> < /html>

输出:
将上面的代码保存在remove.html文件中,然后在新的浏览器中打开该文件。
backbone.js视图remove

文章图片
【backbone.js视图remove】单击后,将得到以下结果。
backbone.js视图remove

文章图片

    推荐阅读