keydown event on a iframe

前端 未结 3 1744
孤街浪徒
孤街浪徒 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.

提交回复
热议问题