BackboneJS - View Remove
描述
它用于从 DOM 中删除视图,并调用 stopListening 来删除视图已 listenTo'd 的任何绑定事件。
语法
view.remove()
示例
<!DOCTYPE html> <html> <head> <title>View Example</title> <script src = "https://code.jquery.com/jquery-2.1.3.min.js" type = "text/javascript"></script> <script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type = "text/javascript"></script> <script src = "https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type = "text/javascript"></script> </head> <body> <div id = "mydiv"></div> <script type = "text/javascript"> //'ViewDemo' 是视图类的名称 var ViewDemo = Backbone.View.extend ({ //当点击事件发生时,它会激活定义的函数"removeFunc" events: { 'click button': 'removeFunc' }, removeFunc: function () { //'remove()' 方法从 DOM 中删除视图 this.remove(); //删除视图后,长度显示为“0” document.write("After removing, view becomes: ",$('#mydiv').length); }, //'render' 提供构造视图所需的逻辑 render: function () { //'$el' 是缓存对象,它推送其中定义的内容,并且 //显示用户应点击以删除视图的按钮 this.$el.html('<button>click to remove</button>'); }, //实例化视图时调用此函数 initialize:function(){this.render();} }); //'myview' 是 'ViewDemo' 类的实例 var myview = new ViewDemo({el: '#mydiv'}); </script> </body> </html>
输出
让我们执行以下步骤来查看上述代码的工作原理 −
将上述代码保存在 remove.htm 文件中。
在浏览器中打开此 HTML 文件。