Click-event on iframe?

后端 未结 2 1179
走了就别回头了
走了就别回头了 2021-01-13 17:36

I have a simple iFrame … 


         


        
2条回答
  •  北海茫月
    2021-01-13 17:59

    jQuery has a method, called .contents(), that when used on an iframe element returns the document of the iframe.

    // Get a reference to the iframe document
    var iframeDoc = $('#inviteFrame').contents().get(0);
    

    Now you can bind an event to it, get the dimensions, get styles of various elements, etc:

    // Get width of iframe document
    $(iframeDoc).width();
    
    // Get height of iframe document
    $(iframeDoc).height();
    
    // Bind event to iframe document
    $(iframeDoc).bind('click', function( event ) {
        // do something
    });
    
    // Get the style of an element in the iframe
    $('div', iframeDoc).css('backgroundColor');
    

提交回复
热议问题