我真正想做的是捕获 iframe 中的 keydown 事件并做其他事情。我在网上搜索,但所有的资料都是关于页面中的 iframe,但我面对的是 iframe 中的 iframe。我的代码如下,当 iframe 未嵌套时它运行良好。谁能给我一些建议吗?
$("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;
});
});
});
最佳答案
代码$("iframe").each()
可以获取嵌套的iframe。也许你可以这样编码 $($("iframe").contentWindow).find("iframe").each(...)
关于javascript - 如何处理嵌套 iframe 中的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9784742/