开发者

Jquery live() with contents inside iframe not working

开发者 https://www.devze.com 2023-04-01 00:46 出处:网络
I have an if开发者_开发问答rame, inside that iframe occurs dom elements additions by ajax. With $(\"iframe\").contents().find() y can access and work with .remove() .html() .click() without problems.

I have an if开发者_开发问答rame, inside that iframe occurs dom elements additions by ajax.

With $("iframe").contents().find() y can access and work with .remove() .html() .click() without problems. But i can't bind events for the new elements added by ajax with live();

my jquery code:

    $("#iframe_id").contents().find("a .tittle").live('click',
        function(e)
        {
              //stuff
        }
    );

But viewing the iframe in a new windows and workinkg with:

$("a .tittle").live('click',function(e)
        {
              //stuff
        }
    );

It works...

Any idea????

Thanks in advice.


find("a .tittle").live('click'

won't work, regardless of iframes. live() can only be called on a direct selector, not one that comes from traversal methods like find. Doc:

DOM traversal methods are not supported for finding elements to send to .live(). Rather, the .live() method should always be called directly after a selector

This is in my opinion a poor, misleading bit of interface design, which is why I'd recommend using the delegate() method in preference to live() in all cases.

Cross-document (into an iframe) event handling is also likely to be a problem. jQuery is not really designed for interacting with documents from code in different documents. I would recommend loading a copy of jQuery into each document, and handling events and DOM manipulation entirely within the code of that document.


First thought

It could be that the iframe contents has not loaded when your try to bind the live handler to it. Try:

$("#iframe_id").load(function(){
    $(this).contents().find("a .tittle").live('click', function(e){
        //stuff
    });
});

I don't think that this is actually the problem.

Second thought

as per the live function docs:

DOM traversal methods are not supported for finding elements to send to .live(). Rather, the .live() method should always be called directly after a selector, as in the example above.

This means you cannot do $('.something').find('.somethingElse').live(...);. Instead you must do $('.something .somethingElse').live(...);

0

精彩评论

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