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