在IE中处理keyPress Accross Frames [英] Handle keyPress Accross Frames in IE

查看:59
本文介绍了在IE中处理keyPress Accross Frames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图通过javascript在多个框架中处理 onkeydown 事件(不,我很遗憾无法摆脱框架)(请参阅我之前的问题here )。我正在另一个框架中处理文档,并将它的onkeydown处理程序设置为等于我的函数。不会引发任何错误,但是当我稍后检查文档的设置时, onkeydown 为空。我在 IE6 IE7 中获得了相同的结果。我做错了什么。

I've been trying to handle the onkeydown event across multiple frames (no, I unfortunately cannot be rid of the frames) via javascript (see my previous question here). I'm getting a handle on the document in the other frame, and setting it's onkeydown handler equal to my function. No error is thrown, but when I later check the document's settings, onkeydown is null. I've gotten the same results in IE6 and IE7. What am I doing wrong.


  • 功能

  • Function

    function setKeyHook(doc)
    {
        try{               
            if (doc)
                if (parent.TOP.handleKeypress){
                    doc.onkeydown = parent.TOP.handleKeypress;
                    logMessage('Attached handler');
                }
                else{
                    logMessage('No handleKeypress');
                }
            else
                logMessage('No doc');
        }
        catch (ex){
            logMessage(ex.toString());
        }
    }


  • 致电

  • Call

    setTimeout(setKeyHook(parent.document.getElementById(\bottom\)。document);,1000);

    setTimeout("setKeyHook(parent.document.getElementById(\"bottom\").document);", 1000);

    输出

    
       Attached handler
    




    • 执行后

    • 
          BOTTOM.protocol = HyperText Transfer Protocol
          BOTTOM.onkeypress = null
          BOTTOM.onrowenter = null
          BOTTOM.onmousedown = null
      

      我应该如何跨帧应用相同的事件处理程序?

      How should I apply the same event handler across frames?

      注意:这需要在IE6和IE7中工作(并且只能工作)。

      Note: This needs to work (and only work) in IE6 and IE7.

      推荐答案

      我让这个工作

      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
         "http://www.w3.org/TR/html4/frameset.dtd">
      <html>
      <head>
          <title>Test</title>
      </head>
      <frameset rows="50%,50%">
          <frame src="frame1.html" name="TOP">
          <frame src="frame2.html" name="BOTTOM">
      </frameset>
      </html>
      



      frame1.html



      frame1.html

      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
         "http://www.w3.org/TR/html4/frameset.dtd">
      <html>
      <head>
          <title>Frame 1</title>
          <script type="text/javascript">
          onload = function()
          {
              top.frames.BOTTOM.document.onkeydown = 
              self.document.onkeydown = function( evt )
              {
                  return function()
                  {
                      // Just an example to show it's working
                      document.getElementById( 'output' ).value += String.fromCharCode( evt.keyCode );
                  }
              }( window.event );
          }
      
          </script>
      </head>
      <body>
          frame1
          <textarea id="output"></textarea>  
      </body>
      </html>
      



      frame2.html



      frame2.html

      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
         "http://www.w3.org/TR/html4/frameset.dtd">
      <html>
      <head>
          <title>Frame 2</title>
      </head>
      <body>
          frame2
          <textarea></textarea>
      </body>
      </html>
      

      首先,对你的对象引用一定要明确付出代价。使用专有的DOM快捷方式(例如window。 frameName )只会增加不必要的错误潜力。这就是我的脚本明确查看窗口对象的 frames 集合的原因。

      First of all, it always pays to be fairly explicit with your object references. Using proprietary DOM shortcuts (such as window.frameName) just add unnecessary error potential. This is why my script explicitly looks in the frames collection of the window object.

      接下来是熟悉各种内置窗口处理框架集时DOM中的引用。共有4个

      Next is to familiarize yourself with the various built-in window references in the DOM when dealing with framesets. There are 4 total


      1. 窗口 - 当前窗口。遗漏时也暗示这一点(即window.onload === onload)

      2. - 当前的父窗口

      3. 顶部 - 框架集系列中最顶层的窗口。当您只有一个框架集时,父级 === 顶部

      4. 自我 - 窗口

      1. window - The current window. This is also implied when left out (i.e., window.onload === onload)
      2. parent - The parent window to the current
      3. top - The topmost window in a frameset family. parent === top when you have only one frameset.
      4. self - alias of window

      所以基本上我在这里做的是,当在frame1窗口中触发onload事件时,为两个框架窗口中的文档的keydown事件添加一个处理函数。

      So basically what I've done here is, when the onload event fires in the frame1 window, is add a handler function to the keydown event for the documents in both of the frame windows.

      该函数使用闭包来确保在frame1中生成的事件可用于实际的处理程序,无论哪个窗口生成了keydown事件。这是必要的,因为IE如何做事件。而其他浏览器在发生新事件对象并将其传递给事件处理程序时,IE只会修改全局事件对象(通过 window.event 引用或更简单,只需事件 )反映当前事件。

      That function uses a closure to ensure that the event generated in frame1 is available to the actual handler, regardless of which window generated the keydown event. This is necessary because of how IE does events. Whereas other browser create new event objects as they occur and passes them to event hanlders, IE just modifies the global event object (referenced via window.event or more simple, just event) to reflect the current event.

      我希望这是有道理的。

      这篇关于在IE中处理keyPress Accross Frames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆