Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript事件onkeypress事件获胜';跑不动_Javascript_Events - Fatal编程技术网

javascript事件onkeypress事件获胜';跑不动

javascript事件onkeypress事件获胜';跑不动,javascript,events,Javascript,Events,我试图在用户按下enter键时提醒他/她。我决定使用javascript来实现这一点。我有以下html代码: <div id="all"> <h1>Welcome to the awesome chat system.</h1> <input type="text" id="name"><br/> <textarea rows='30' id="chatAr

我试图在用户按下enter键时提醒他/她。我决定使用javascript来实现这一点。我有以下html代码:

   <div id="all">
           <h1>Welcome to the awesome chat system.</h1>
           <input type="text" id="name"><br/>
           <textarea  rows='30' id="chatArea" disabled='true'>
           </textarea>
           <textarea id="writtenThing" onkeypress="keyfunction(e)"></textarea>
           <button id="send">Send</button>
    </div>

欢迎来到很棒的聊天系统。

发送
下面是javascript:

       <script>
            function keyfunction(e)
            {

                if(e.keyCode == 13)
                {
                    alert("things to do...");
                }

            }

       </script>

功能键功能(e)
{
如果(e.keyCode==13)
{
警惕(“要做的事情…”);
}
}

然而,我面临的问题是,即使我在文本区域内按enter键,我的浏览器也不会提醒我。我确信我使用的浏览器支持此功能。

您可以将onkeypress替换为onkeyup/onkeydown

此外,请使用以下内容更正代码:


onkeypress=“keyfunction(event)”

将事件传递给函数:

   <textarea id="writtenThing" onkeyup="keyfunction(event)"></textarea>

JS Fiddle:

使用以下脚本

 document.getElementById("writtenThing").onkeypress = function (e) 
 {
    if (e.keyCode == 13)
  {
     alert("things to do...");
  }
  }

我更改了它,但是它没有提醒您正在函数中传递“e”…而是在KeyPress=“keyfunction(event)”上传递“event”
 document.getElementById("writtenThing").onkeypress = function (e) 
 {
    if (e.keyCode == 13)
  {
     alert("things to do...");
  }
  }