Firefox 和 IE8 中的 onKeyPress 事件 [英] onKeyPress event in Firefox and IE8

查看:21
本文介绍了Firefox 和 IE8 中的 onKeyPress 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Javascript 代码.这里我在 body 标签中使用 onKeyPress="someFunction( )" 来获取按下的键的 keyCode.

代码在 IE8 中运行良好,但 在 Firefox 中不起作用.

请给出一些解决方案.

<头><title>onKeyPress() 事件在 Firefox 中不起作用..</title><脚本>函数printDiv(){var divToPrint=document.getElementById('prnt');newWin=window.open(''+self.location,'PrintWin','left=50,top=20,width=590,height=840,toolbar=1,resizable=1,scrollbars=yes');newWin.document.write(divToPrint.outerHTML);newWin.print();//newWin.close();}<脚本>功能按键(){警报(事件.keyCode);var key=event.keyCode;if(key==112 || key==80)打印Div();否则 if(key==101 || key==69)window.location="http://google.com";否则 if(key==114 || key==82)window.reset();}<body bgcolor="lightblue";onkeypress="keypress()">


这是在 IE8 中运行良好但在 Firefox 中无法运行的总代码.

<头><title>请帮帮我</title><script type="text/javascript">函数printDiv(){var divToPrint=document.getElementById('prnt');newWin=window.open(''+self.location,'PrintWin','left=50,top=20,width=590,height=840,toolbar=1,resizable=1,scrollbars=yes');newWin.document.write(divToPrint.outerHTML);newWin.print();}<script type="text/javascript">功能按键(val){//-----------------------------------------------------//警报('nnnn');//alert(window.event ? event.keyCode : val.which);//if(val.which != 0 && val.charCode != 0)//alert('Firefox'+String.fromCharCode(val.which));//别的//警报('IE');//-------------------------------------------------------var key=event.keyCode;if(key==112 || key==80 || val==打印")打印Div();else if(key==101 || key==69 || val=="exit")window.location="http://google.co.in";else if(key==114 || key==82 || val==刷新")document.forms[0].reset();别的event.returnValue=true;}<body bgcolor="lightblue";topmargin=0";leftmargin=0"marginwidth=0px"边距高度=0px";onkeypress="keypress(null)"><表格对齐=左"边界=1"单元格填充=5"单元格间距=0"样式=宽度:100%;高度:100%"><tr><td宽度=20%";valign=top">ccccccccccc</td><td宽度=80%";对齐=中心"><表格样式=宽度:100%"边界=0";valign=顶部"><tr align="right"><td valign="top"><按钮值=刷新"访问密钥=R"onclick="keypress(this.value)"><b><u>R</u></b>新鲜<按钮值=打印"访问密钥=P"onclick="keypress(this.value)">&nbsp;&nbsp;<b><u>P</u></b>rint&nbsp;&nbsp;<按钮值=退出"访问密钥=E"onclick="keypress(this.value)">&nbsp;&nbsp;<b><u>E</u></b>xit&nbsp;&nbsp;&nbsp;</td></tr><h3>按字母 P->打印、E->退出等...</h3><h1>只是按键事件的测试</h1><form action="http://google.co.in";方法=发布"><div id="prnt">呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜

</表单></td></tr></tbody></table></body></html>

解决方案

当出现此类问题时,我开始使用任何类型的 JavaScript 框架.这些框架的构建是为了避免不同浏览器的问题.

捕捉所有不同的 keypress() api,就像 Emmett 显示的链接一样,可能非常困难.

示例:

在 HTML 头部:

<script src="http://code.jquery.com/jquery-1.4.4.js"></script>

在 JS 标签中:

$(document).keydown(function(event) {alert('你按下了'+event.keyCode);event.preventDefault();});

I have the following Javascript code. Here I am using onKeyPress="someFunction( )" in the body tag to get the keyCode of the key that is pressed.

The code is working fine in IE8 but this is not working in Firefox.

Please give some solution to this.

<html>
<head>
<title>onKeyPress( ) event not working in firefox..</title>
<script>
function printDiv()
{
  var divToPrint=document.getElementById('prnt');
  newWin=window.open(''+self.location,'PrintWin','left=50,top=20,width=590,height=840,toolbar=1,resizable=1,scrollbars=yes');
  newWin.document.write(divToPrint.outerHTML);
  newWin.print();
  //newWin.close();
}
</script>

<script>
function keypress()
{
  alert(event.keyCode);
  var key=event.keyCode;
  if(key==112 || key==80)
 printDiv();
  else if(key==101 || key==69)
    window.location="http://google.com";
  else if(key==114 || key==82)
    window.reset();  
}
</script>
</head>
<body bgcolor="lightblue" onkeypress="keypress()">


Here is the total code which is working fine in IE8 and not working in Firefox.

<!DOCTYPE html>
<html>
    <head>
    <title>Please help me out</title>
    <script type="text/javascript">
    function printDiv()
    {
      var divToPrint=document.getElementById('prnt');
      newWin=window.open(''+self.location,'PrintWin','left=50,top=20,width=590,height=840,toolbar=1,resizable=1,scrollbars=yes');
      newWin.document.write(divToPrint.outerHTML);
      newWin.print();
    }
    </script>

    <script type="text/javascript">
    function keypress(val)
    {
      //-----------------------------------------------------   
      //alert('nnnn');
      //alert(window.event ? event.keyCode : val.which);  
      //if(val.which != 0 && val.charCode != 0)
      // alert('Firefox'+String.fromCharCode(val.which));
      //else
      // alert('IE'); 
      //------------------------------------------------------- 
      var key=event.keyCode;
      if(key==112 || key==80 || val=="print")
        printDiv();
      else if(key==101 || key==69 || val=="exit")
        window.location="http://google.co.in";
      else if(key==114 || key==82 || val=="refresh")
        document.forms[0].reset();  
      else
        event.returnValue=true;
    }
    </script>
</head>
<body bgcolor="lightblue" topmargin="0" leftmargin="0"marginwidth="0px" marginheight="0px" onkeypress="keypress(null)">
<table align="left" border="1" cellpadding="5" cellspacing="0" style="width: 100%;height:100%">
<tbody>
<tr><td width="20%" valign="top">ccccccccccc</td>
    <td width="80%" align="center">
        <table style="width: 100%" border="0" valign="top">
        <tr align="right">
        <td valign="top">
        <button value="refresh" accesskey="R" onclick="keypress(this.value)">
            <b><u>R</u></b>efresh
        </button>
        <button value="print" accesskey="P" onclick="keypress(this.value)">
            &nbsp;&nbsp;<b><u>P</u></b>rint&nbsp;&nbsp;
        </button>
        <button value="exit" accesskey="E" onclick="keypress(this.value)">
            &nbsp;&nbsp;&nbsp;<b><u>E</u></b>xit&nbsp;&nbsp;&nbsp;
        </button>
        </td></tr>
        </table> 
        <h3>Press the letters P->Print , E->Exit etc....</h3>   
        <h1>Just a test for keypress event</h1>
        <form action="http://google.co.in" method="Post">
            <div id="prnt">
                zzzzzzzzzzzzzzz
            </div>
        </form>
    </td>
</tr>
</tbody>
</table></body></html>

解决方案

When problems like this show up, I start to use any kind of a JavaScript framework. Those frameworks are build to avoid problems with different browsers.

To catch all different keypress() apis, like the link from Emmett shows, can be very difficult.

Example:

In HTML head:

<script src="http://code.jquery.com/jquery-1.4.4.js"></script>

In the JS tag:

$(document).keydown(function(event) {
 alert('You pressed '+event.keyCode);
 event.preventDefault();
});

这篇关于Firefox 和 IE8 中的 onKeyPress 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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