Adding event handler to an iframe using JQuery

后端 未结 4 732
广开言路
广开言路 2020-12-01 19:07

I want to assign a keydown event handler to an iframe. Something similar to the pure JS:

document.getElementById(\'iframe_id\').contentWindow.addEventListene         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 20:07

    contentWindow is the iframe's window object. You want the iframe's document instead:

    $(document.getElementById('iframe_id').contentWindow.document).keydown(function() {
        // my func
    });
    

    Note that I am not sure how jQuery reacts to elements from other windows/frames.

提交回复
热议问题