开发者

How to mimic jQuery.live(), or use 2 jQuery versions in the presence of a Prototype library?

We are rendering our content inside a HTML 'wrapper' which is supplied by the client. That HTML wrapper contains a reference to jQuery 1.2.6 so that's what I've been using for my jQuery functionality up until now.

The problem I'm facing is that I'm dynamically rendering content that needs a click event associated with it. The click event is associated with the elements on $(document).ready(). As such, I need functionality of the .live() function which is available in version 1.3+.

So I'm wondering what are my options?

Is there any way for me to easily mimi开发者_开发问答c the functionality of .live() so that I don't need the function supplied by the jQuery library?

Do I need to include the new jQuery library with our content? This is not ideal because it causes conflicts which need to be managed, and we're already managing conflicts with a Prototype library which somebody else has included in the wrapper with the following line:

jQuery(document).ready(function ($) {

unless somebody can show me an easy way of doing this?

... or do I put it back to the client that it's time for them to upgrade their jQuery? I don't know if it's likely that this will happen.

Can anyone suggest a solution to this problem? Thanks


You could simply roll your own event delegation. For example, rather than binding to the dynamically created elements, e.g.:

$('.foo').click(function() { /* foo handler */ });

You can instead attach a handler to a higher level element, like the document, and filter the click events:

$(document).click(function(evt) {
    var $target = $(evt.target);
    if ($target.is('.foo') || $target.parents('.foo').length > 0) {
        /* foo handler */
    }
});

This is the basic idea that live (and the newer delegate) operate on.


*edited example to check the event target's parent elements as well


Can you use the livequery plugin? Should work with that version.

http://plugins.jquery.com/project/livequery/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜