gpt4 book ai didi

javascript - JS 事件处理,停留在基础知识上

转载 作者:行者123 更新时间:2023-12-02 02:27:20 25 4
gpt4 key购买 nike

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

脚本的用途:通过将鼠标悬停在提交按钮上,在发送到服务器之前应用 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();

表单.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 与表单 inputs 一起使用。您必须改用 .value 属性。尝试下面的代码:) 另外,对于 mouseover 事件,只需使用 assignRots 而不是 assignRots()

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);

关于javascript - JS 事件处理,停留在基础知识上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65454044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com