开发者

dynamically added HTML elements won't be affected by plugins !

开发者 https://www.devze.com 2022-12-28 14:30 出处:网络
there are plugins such as flowplayer overlay that asks to put a \"rel\" attribute to the HTML element to make it trigger certai开发者_JS百科n events ... the problem is , when I create dynamically elem

there are plugins such as flowplayer overlay that asks to put a "rel" attribute to the HTML element to make it trigger certai开发者_JS百科n events ... the problem is , when I create dynamically elements that have that rel attribute ,, they won't trigger it ... what is the solution for this !?


You should use the live() method to trigger events for dynamically created elements.

Example:

$('selector').live('click', function(){
  // your code .........................
});


Since jQuery 1.4.2 you can also use .delegate():

Description: Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.

The use of delegate can sometimes result in less and more understandable code than .live().


Edit the plugin so it uses live() to handle events.


Generally, there is the jQuery live() function to address exactly this kind of issue.

You would have to change the plugin to use live(), or run their initialization function every time new content is added.

The latter approach can be pretty resource-intensive on the client end and is not very clean, but if the number of elements in question is not too much, it can often be the easiest way.

0

精彩评论

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