简体   繁体   English

如何动态添加单击附加事件

[英]How to add the click attach event dynamically

I need to attach the click event dynamically using JavaScript. 我需要使用JavaScript动态附加click事件。 My sample code is given below. 我的示例代码如下。

require(["dijit/form/ToggleButton", "dojo/dom-construct"], function (ToggleButton, domConstruct) { 
    var newButton = new ToggleButton({
        showLabel: true,
        checked: false,
        onChange: function (val) { frame(this); },
        label: item.getAttribute('label')
    }, item.getAttribute('id'));
});

If you are using dojo 1.8+ you can use Widget#on to connect to events after the widget is created. 如果您使用的是dojo 1.8+,则可以Widget#on创建窗口小部件后使用Widget#on连接事件。

var newButton = new ToggleButton({
    showLabel: true,
    checked: false,
    label: item.getAttribute('label')
}, item.getAttribute('id'));

newButton.on('change',function(){
    console.log('onChange event called');
});

newButton.on('click',function(){
    console.log('click event called');
});

use button id attribute to attach click event like 使用button id属性来附加点击事件

$("#buttonid").click(function(){

//code goes here
})

where buttonid is id attribute of your button 其中buttonid是按钮的id属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM