开发者

Problem connecting an event to dynamically added dom element with dojo

开发者 https://www.devze.com 2023-03-21 18:09 出处:网络
I am having a problem with connecting a click event to a dynamically added dom element. I add a new dom element lik开发者_开发技巧e this:

I am having a problem with connecting a click event to a dynamically added dom element. I add a new dom element lik开发者_开发技巧e this:

var link = dojo.create("a",{
                      className: "deleteDnd",
                      innerHTML: "Delete"
                    },this.domNode,"first");

I would now like to add a click event to this dynamically added link, however I have tried to add the event numerous times but it fails. I am able to connect the event using firebug after the element has been appended. Is there a way to connect the event whilst creating the element something like :

var link = dojo.create("a",{
                      className: "deleteDnd",
                      innerHTML: "Delete"
                    },this.domNode,"first");
dojo.event.connect(link, "onclick", function(node, index, nodelist){
                        console.log("was clicked");

                    });

Any help would be greatly appreciated.


The function dojo.event.connect doesn't exist anymore in the newer versions of Dojo. Try using just dojo.connect instead.

dojo.connect(link, "onclick", function(node, index, nodelist){
        console.log("was clicked");
        console.log(node, index, nodelist); // Only first argument is defined
    });

You'll also notice that the link's event handler function will only get one argument (the event), not node, index and nodeList.

0

精彩评论

暂无评论...
验证码 换一张
取 消