如何绑定在骨干动态生成的按键事件? [英] How to bind events on dynamic generated buttons in backbone?
问题描述
如何绑定在Backbone.js的点击动态生成按钮事件?
How do I bind click event on dynamic generated buttons in backbone.js?
window.PackageView = Backbone.View.extend({
tagName: "div",
className: "package-template",
events:{
'click #display-nodes' : 'main', // This button is declared in my HTML code and calls main method successfully.
'click .display' : 'disp', // This is dynamic button generated with class as display
},
getAction: function(nodeId){ // Get Actions from NodeId and generate buttons
$('.nodes').append("<button>" + action.Name + "</button>"); //Generate Buttons
$(".nodes button").addClass("display");
},
disp: function(){
alert("Inside Disp Function");
},
在点击#显示节点
的要求,但。显示
工作不显示这些节点。如何使这个按钮调用该函数?
On clicking #display-nodes
the nodes are displayed as required but .display
is not working. How do I make this button call the function?
推荐答案
一个骨干视图只要能接收来自动态生成的DOM元素的事件,通过事件
属性,动态生成的DOM元素视图的报
的后裔。有关code在 delegateEvents()。它使用jQuery的委托()的选择方法。
A Backbone view can receive events from dynamically generated DOM elements, through the events
property, as long as the dynamically generated DOM elements are descendants of the view's el
. The relevant code is in delegateEvents(). It uses jQuery's delegate() selector method.
它不为你工作最可能的原因是,新创建&LT;按钮&GT;
不是视图的报<后裔/ code>。我说得对不对?
The most likely reason it's not working for you is that the newly created <button>
is not a descendant of the view's el
. Am I right?
如果是这样的话,你要保留的元素视图的 EL外
,您可以从您的视图的事件删除
财产和代表在您的视图的的init
方法。
If this is the case and you want to keep the element outside of the view's el
, you can remove it from your view's events
property and delegate on another element in your view's init
method.
这篇关于如何绑定在骨干动态生成的按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!