Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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 JS事件处理,停留在基础上_Javascript - Fatal编程技术网

Javascript JS事件处理,停留在基础上

Javascript JS事件处理,停留在基础上,javascript,Javascript,由于网络主机的更改,我无法再使用内联脚本,因此我正在将一些脚本迁移到外部文件。我似乎无法让这一个开火: 脚本的用途:将rot13()应用于表单数据,然后将其发送到服务器,方法是将鼠标悬停在“提交”按钮上 rot13.js function rot13(s) { return s.replace(/[A-Z]/gi, c => "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"[

由于网络主机的更改,我无法再使用内联脚本,因此我正在将一些脚本迁移到外部文件。我似乎无法让这一个开火:

脚本的用途:将rot13()应用于表单数据,然后将其发送到服务器,方法是将鼠标悬停在“提交”按钮上

rot13.js

   function rot13(s) {
     return s.replace(/[A-Z]/gi, c =>
       "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"[
       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".indexOf(c) ] )
   }
   
   function assignRots()
   {
      var new_auth = document.getElementById("author").innerHTML;
      document.getElementById("author").innerHTML = rot13(new_auth);
      
      var new_email = document.getElementById("email").innerHTML;
      document.getElementById("email").innerHTML = rot13(new_email);
      
      var new_msg = document.getElementById("msg").innerHTML;
      document.getElementById("msg").innerHTML = rot13(new_msg);
   }
   
   document.getElementById("submit").onmouseover = assignRots();
form.html

   <form id="newCommentForm" method="post" action="" enctype="multipart/form-data">

   <input name="author" id="author" type="text" value="" size="30" maxlength="50" />
   
   <input name="email" id="email" type="text" value="" size="35" maxlength="50" />
   
   <textarea name="msg" id="msg" rows="12" cols="80" maxlength="4000"></textarea>
   
   <input type="submit" id="submit" name="submit" value="Submit">

   </form>

   <script type="text/javascript" src="rot13.js"></script>

您不应将
innerHTML
与表单
输入一起使用。您必须改用
.value
属性。尝试以下代码:)也可以使用
mouseover
事件,只需使用
assignRots
而不是
assignRots()


onmouseover
需要一个函数,而不是将它分配给函数的返回值

尝试:

function assignRots()
   {
      var new_auth = document.getElementById("author").value;
      document.getElementById("author").value= rot13(new_auth);
      
      var new_email = document.getElementById("email").value;
      document.getElementById("email").value= rot13(new_email);
      
      var new_msg = document.getElementById("msg").value;
      document.getElementById("msg").value= rot13(new_msg);
   }

document.getElementById("submit").addEventListener("mouseover",assignRots); 
document.getElementById("submit").onmouseover = assignRots;