如何在主干中动态生成的按钮上绑定事件? [英] How to bind events on dynamic generated buttons in backbone?

查看:25
本文介绍了如何在主干中动态生成的按钮上绑定事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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");
    },

点击#display-nodes 时,节点会根据需要显示,但.display 不起作用.如何让这个按钮调用函数?

On clicking #display-nodes the nodes are displayed as required but .display is not working. How do I make this button call the function?

推荐答案

Backbone 视图可以通过 events 属性从动态生成的 DOM 元素接收事件,只要动态生成的 DOM 元素是视图的 el 的后代.相关代码在 delegateEvents() 中.它使用 jQuery 的 delegate() 选择器方法.

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.

它不适合您的最可能原因是新创建的

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆