简体   繁体   English

如何处理嵌套iframe中的事件?

[英]How to handle events in nested iframe?

What I really want to do is to capture the keydown event in iframe and do something else. 我真正想做的是捕获iframe中的keydown事件并执行其他操作。 I search on the Internet but all the materials are about iframe in a page but what I am faced are iframes in iframe. 我在Internet上搜索,但是所有材料都是关于页面中iframe的,但是我所面对的是iframe中的iframe。 My code are below and it works well when the iframes are not nested. 我的代码在下面,当不嵌入iframe时,效果很好。 Can anyone give me some advices? 谁能给我一些建议?

$("iframe").each( function(i) {             
        var currentIframe=this;                     
        $(this).load(function(event) {
            var iFrameDoc =currentIframe.contentDocument || currentIframe.contentWindow.document;                       

            $(iFrameDoc).find("input").each( function(i) {

                $(this).keydown(function(event) {                                   
                  event.stopPropagation();      
                });                     
            });

            $(iFrameDoc).keydown(function(event) {                  
                var code = (event.keyCode ? event.keyCode : event.which);   
                var e = jQuery.Event("keydown");                
                e.which = code; // # Some key code value    
                $input.trigger(e);  
            }); 

            $(iFrameDoc).mousemove(function(event) {    
                        currentMousePos.x=currentIframe.offsetLeft+event.pageX; 
                        currentMousePos.y=currentIframe.offsetTop+event.pageY;  
            });                         

        });                 
});

The code $("iframe").each() can get the nested iframe. 代码$("iframe").each()可以获取嵌套的iframe。 May be you can code like this $($("iframe").contentWindow).find("iframe").each(...) 也许您可以像这样$($("iframe").contentWindow).find("iframe").each(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM