keydown event on a iframe

前端 未结 3 1729
孤街浪徒
孤街浪徒 2021-01-02 00:35

How i can get keydown event on iframe?

相关标签:
3条回答
  • 2021-01-02 00:52

    It's so late to answer but maybe is useful for somebody .
    I've same problem,that means I've an iframe inside a HTML page in a same domain.
    Now I need to get the Esc keyup event.
    however I don't want (or some times I can't) to write any script inside iframe body.
    to do it, I use below script to detect what is the parent id of element that selected by clicked on it.

     $(window).load(function () {
                jQuery(jQuery('[id="frame"]')[0].contentWindow.document).on('keyup', function (e) {
    
                    var _currntItem = ($(e.target))
    
                    if (e.keyCode === 27) {
                        alert(_currntItem.parent().attr('id'));
                    }
                }); 
    

    This code resolved my problem.
    hope to useful.

    0 讨论(0)
  • 2021-01-02 01:02

    Adding event handler to an iframe using JQuery

    $(document.getElementById('iframe_id').contentWindow.document).keydown(function() {
        // my func
    });
    
    0 讨论(0)
  • 2021-01-02 01:07

    You need to access the document of the iframe which is through the ContentWindow object

    $(document.getElementById('IFrameId').contentWindow.document).keydown(function(){ alert('Key down!'); });
    

    Make sure you bind the event after the frame has loaded.

    0 讨论(0)
提交回复
热议问题