开发者

How to append jQuery events to new elements?

开发者 https://www.devze.com 2023-04-11 10:05 出处:网络
I have a SVG canvas and when I double-click on it, I create a new SVG element inside it. How I do to create a click event for these new elements created at real time by the id of the new element.

I have a SVG canvas and when I double-click on it, I create a new SVG element inside it.

How I do to create a click event for these new elements created at real time by the id of the new element.

These elements ids will be a word I inform.

The elements are created by an external API (Graph Dracula), so can't change it.

Example开发者_开发问答: I add a new element called "Renato", so the element id will be "Renato".


It sounds like you should just be able to bind it using something like:

$("#...").click(function() { ... });

Basically it's like normal. You just need some kind of reference to it and you can use jQuery to bind something on it. If you're creating it, you obviously have a reference to it then, so you could do it at that step.

Edit: just read that they're created by an external API. However, if you know the id, you can always just use $("#idhere").


You would want to use the live function in jQuery. It can tie events to dynamically created elements after the DOM has loaded.

$("#Renato").live("click", function(){...});

Or in later versions of jQuery

$('#some_non_dynamic_parent').on('click', '#Renato', function(){});
0

精彩评论

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