【问题标题】:Capture onkeydown in top window在顶部窗口中捕获 onkeydown
【发布时间】:2011-04-24 22:30:54
【问题描述】:

我尝试在包含多个 iframe 的 iframe 的窗口中捕获 onkeydown 事件:

top.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <title> top.html </title>
</head>
<body>
  <iframe frameborder="0" border="0" width="100%" height="100%" src="framset1.html" id="framset1"></iframe>
</body>

frameset1.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
   "http://www.w3.org/TR/html4/frameset.dtd">
<HTML>
<HEAD>
<TITLE>A frameset document</TITLE>
</HEAD>
  <FRAMESET rows="66,*" border="0">
      <FRAME scrolling="no" id="frame1" name="frame1" src="frame1.html">
      <FRAME id="framset2" name="framset2" src="framset2.html">
  </FRAMESET>
</HTML>

frameset2.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
   "http://www.w3.org/TR/html4/frameset.dtd">
<HTML>
<HEAD>
<TITLE>A frameset document</TITLE>
</HEAD>
  <FRAMESET cols="46,*" border="0">
      <FRAME scrolling="no" id="frame2" name="frame2" src="frame2.html">
      <FRAME id="frame3" name="frame3" src="frame3.html">
  </FRAMESET>
</HTML>

我想在 frame3 中捕获事件 onkeydown。我在 stackoverflow here 上找到了一个相关问题,但没有成功。

我是否必须继续这样做,否则任何人都可以帮助我。

谢谢。

保罗

【问题讨论】:

    标签: javascript events iframe frameset onkeydown


    【解决方案1】:

    如果你想在顶部框架中分配事件监听器(top.html):

    document.getElementById('framset1').contentDocument.getElementById('framset2').contentDocumentcontentDocument.getElementById('framset3').contentDocument.addEventListener('keydown', function(e) {
      alert('keydown in frame 700');
    }, false);
    

    这仅适用于所有帧都在同一个域 + 端口上的情况。否则,您将在尝试访问 document.getElementById('framset1').contentDocument 时遇到安全异常。

    但是框架中的框架中的框架......这真的是你想要的吗??

    【讨论】:

    • 谢谢鲁迪。你是对的......框架内的框架内的框架......这很丑陋。我想在这个旧应用程序上添加一些功能。替代品的开发被延迟(我没有开发)。我需要更多的时间和手段进行重构。我不能投票给你的答案...再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多