Bind to events inside dynamically created iframe

后端 未结 1 1549
逝去的感伤
逝去的感伤 2020-12-14 10:09

I need to bind to an event (say a click on an arbitrary ) inside an iframe that is created dynamically after the user performs a certa

相关标签:
1条回答
  • 2020-12-14 10:26

    This 'iframe input' does not selects input elements inside the iframe.

    You can bind the event like

    $('body iframe').contents().find('input').bind('click',function(e) {
        alert('bingo?');
     });
    

    I think You can also use something like

    $('body iframe').contents().find('body').delegate('input','click',function(e) {
        alert('bingo?');
     });
    

    To detect if the iframe has been fully loaded, use the method described in this answer https://stackoverflow.com/a/5788723/344304

    Add In the main/parent document:

    function iframeLoaded() {
        $('body iframe').contents().find('input').bind('click',function(e) {
            alert('bingo?');
         });
    }
    

    Add In the iframe document:

    window.onload = function() {
        parent.iframeLoaded();
    }
    

    Or use

    $('body iframe').load(function(){
         $('body iframe').contents().find('input').bind('click',function(e) {
                alert('bingo?');
             });
    });
    
    0 讨论(0)
提交回复
热议问题