onKeyPress事件在Firefox中不起作用

时间:2010-12-21 06:56:08

标签: javascript firefox javascript-events onkeypress

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

代码在IE8中工作正常,但在Firefox中无效。

请给出一些解决方法。

<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()">

提前致谢.....


以下是在IE8中正常运行但在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>

4 个答案:

答案 0 :(得分:12)

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

要捕捉所有不同的keypress() api,就像Emmett节目中的链接一样,可能非常困难。

示例:

在HTML头中:

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

在JS标签中:

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

答案 1 :(得分:8)

浏览器有不同的处理键盘事件的方法。有关详细信息,请查看http://unixpapa.com/js/key.html

例如,对代码的这些更改将使其在Firefox中运行:

<body bgcolor="lightblue" onkeypress="keypress(e)">

function keypress(e) {
    alert(window.event ? event.keyCode : e.which);
    // other stuff
}

答案 2 :(得分:6)

将事件对象作为参数传递,您的代码将在IE和firefox中运行。代码示例如下:

<body bgcolor="lightblue" onkeypress="keypress(event)">
function keypress(event) {
  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();  
}

答案 3 :(得分:0)

我认为Firefox不关心程序员...... 这就是为什么这样的原因, 在Firefox中,navigator.appName返回“Netscape”。 所以用户可以编辑他的代码,如

if(navigator.appName == "Netscape") 
    Key = event.charCode; //or e.which; (standard method)
else 
    Key = event.keyCode;