Javascript innerHTML正在处理body元素,而不是p元素
我想在屏幕上显示一条消息,如果电子邮件地址无效,则显示“请输入有效的电子邮件地址”。body元素的innerHTML语句工作正常,但我用于p元素的语句不工作 有一次,当我测试它时,我看到显示“请输入有效的电子邮件地址”的消息,然后在我单击“无效”警报框的“确定”按钮后,消息消失了 JavaScript:Javascript innerHTML正在处理body元素,而不是p元素,javascript,html,innerhtml,Javascript,Html,Innerhtml,我想在屏幕上显示一条消息,如果电子邮件地址无效,则显示“请输入有效的电子邮件地址”。body元素的innerHTML语句工作正常,但我用于p元素的语句不工作 有一次,当我测试它时,我看到显示“请输入有效的电子邮件地址”的消息,然后在我单击“无效”警报框的“确定”按钮后,消息消失了 JavaScript: <script type="text/javascript"> function validateEmail() { var emailRule = /^(([
<script type="text/javascript">
function validateEmail() {
var emailRule = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (emailRule.test(document.forms[0].email.value)) {
document.getElementById("body").innerHTML = "Thank you for signing up for our newsletter! You will recieve a confirmation email shortly.";
setTimeout("window.close()", 3000);
}
else
window.alert("not valid"); //for debugging
document.getElementById("message").innerHTML = "Please enter a valid email address";
}
</script>
</head>
<body id="body">
<p>If you sign up for our newsletter you will be entered into a drawing to win free clothes every month!</p>
<br>
<p id="message"> </p>
<form action="" onsubmit="validateEmail();">
<p>Email: <input type="text" name="email"/>
<input type="submit" value="Sign up"/></p>
<br>
<input type="button" value="No thanks" onclick="window.close()"/>
<br>
</form>
<p><a href="privacy.html">Privacy Policy</a></p>
</body>
</html>
函数validateEmail(){
var emailRule=/^([^()[\]\\,;:\s@\“]+(\.[^()[\]\,;:\s@\“]+)*(\“+\”)((\[[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[1,3}])){124;([a-zA Z-0-9]+-Z]+-[a-2]/];
if(emailRule.test(document.forms[0].email.value)){
document.getElementById(“body”).innerHTML=“感谢您注册我们的时事通讯!您将很快收到一封确认电子邮件。”;
setTimeout(“window.close()”,3000);
}
其他的
window.alert(“无效”);//用于调试
document.getElementById(“message”).innerHTML=“请输入有效的电子邮件地址”;
}
HTML:
<script type="text/javascript">
function validateEmail() {
var emailRule = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (emailRule.test(document.forms[0].email.value)) {
document.getElementById("body").innerHTML = "Thank you for signing up for our newsletter! You will recieve a confirmation email shortly.";
setTimeout("window.close()", 3000);
}
else
window.alert("not valid"); //for debugging
document.getElementById("message").innerHTML = "Please enter a valid email address";
}
</script>
</head>
<body id="body">
<p>If you sign up for our newsletter you will be entered into a drawing to win free clothes every month!</p>
<br>
<p id="message"> </p>
<form action="" onsubmit="validateEmail();">
<p>Email: <input type="text" name="email"/>
<input type="submit" value="Sign up"/></p>
<br>
<input type="button" value="No thanks" onclick="window.close()"/>
<br>
</form>
<p><a href="privacy.html">Privacy Policy</a></p>
</body>
</html>
如果您注册我们的时事通讯,您将进入抽奖,每月赢得免费衣服
电邮:
函数validateEmail(){
var emailRule=/^([^()[\]\\,;:\s@\“]+(\.[^()[\]\,;:\s@\“]+)*(\“+\”)((\[[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[1,3}])){124;([a-zA Z-0-9]+-Z]+-[a-2]/];
if(emailRule.test(document.forms[0].email.value)){
document.getElementById(“body”).innerHTML=“感谢您注册我们的时事通讯!您将很快收到一封确认电子邮件。”;
setTimeout(“window.close()”,3000);
}
其他的
{
window.alert(“无效”);//用于调试
document.getElementById(“message”).innerHTML=“请输入有效的电子邮件地址”;
}
}
如果您注册我们的时事通讯,您将进入抽奖,每月赢得免费衣服
电邮:
函数validateEmail(){
var emailRule=/^([^()[\]\\,;:\s@\“]+(\.[^()[\]\,;:\s@\“]+)*(\“+\”)((\[[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[1,3}])){124;([a-zA Z-0-9]+-Z]+-[a-2]/];
if(emailRule.test(document.forms[0].email.value)){
document.getElementById(“body”).innerHTML=“感谢您注册我们的时事通讯!您将很快收到一封确认电子邮件。”;
setTimeout(“window.close()”,3000);
}
其他的
{
window.alert(“无效”);//用于调试
document.getElementById(“message”).innerHTML=“请输入有效的电子邮件地址”;
}
}
如果您注册我们的时事通讯,您将进入抽奖,每月赢得免费衣服
电邮:
表单未提交任何内容,因为您没有阻止默认操作。这就是消息出现(工作)和消失(重新加载页面)的原因
表单没有提交任何内容,因为您没有阻止默认操作。这就是消息出现(工作)和消失(重新加载页面)的原因
首先,如果要保留console.log,请在else语句中添加大括号-它可以在没有大括号的情况下工作,但只有一行after受影响,而后面有两行,因此无论if/else如何,都可以更改innerHTML 此外,如果出现else语句,请添加Javascript的以停止表单提交:
else {
event.preventDefault();
document.getElementById("message").innerHTML = "Please enter a valid email address";
}
首先,如果要保留console.log,请在else语句中添加大括号-它可以在没有大括号的情况下工作,但只有一行after受影响,而后面有两行,因此无论if/else如何,都可以更改innerHTML 此外,如果出现else语句,请添加Javascript的以停止表单提交:
else {
event.preventDefault();
document.getElementById("message").innerHTML = "Please enter a valid email address";
}
无论报表的结果如何,表单都将提交。代码正在工作,但表单submit正在重新加载页面。添加
返回false代码>修复了该问题:
<script type="text/javascript">
function validateEmail() {
var emailRule = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (emailRule.test(document.forms[0].email.value)) {
document.getElementById("body").innerHTML = "Thank you for signing up for our newsletter! You will recieve a confirmation email shortly.";
setTimeout("window.close()", 3000);
}
else
window.alert("not valid"); //for debugging
document.getElementById("message").innerHTML = "Please enter a valid email address";
return false; // this stops the form from submitting, and the page from reloading.
}
</script>
</head>
<body id="body">
<p>If you sign up for our newsletter you will be entered into a drawing to win free clothes every month!</p>
<br>
<p id="message"> </p>
<form action="" onsubmit="validateEmail();">
<p>Email: <input type="text" name="email"/>
<input type="submit" value="Sign up"/></p>
<br>
<input type="button" value="No thanks" onclick="window.close()"/>
<br>
</form>
<p><a href="privacy.html">Privacy Policy</a></p>
</body>
</html>
函数validateEmail(){
var emailRule=/^([^()[\]\\,;:\s@\“]+(\.[^()[\]\,;:\s@\“]+)*(\“+\”)((\[[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[1,3}])){124;([a-zA Z-0-9]+-Z]+-[a-2]/];
if(emailRule.test(document.forms[0].email.value)){
document.getElementById(“body”).innerHTML=“感谢您注册我们的时事通讯!您将很快收到一封确认电子邮件。”;
setTimeout(“window.close()”,3000);
}
其他的
window.alert(“无效”);//用于调试
document.getElementById(“message”).innerHTML=“请输入有效的电子邮件地址”;
return false;//这将停止提交表单,并停止重新加载页面。
}
如果您注册我们的时事通讯,您将进入抽奖,每月赢得免费衣服
电邮:
无论报表结果如何,表单都将提交。代码正在工作,但表单submit正在重新加载页面。添加返回false代码>修复了该问题:
<script type="text/javascript">
function validateEmail() {
var emailRule = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (emailRule.test(document.forms[0].email.value)) {
document.getElementById("body").innerHTML = "Thank you for signing up for our newsletter! You will recieve a confirmation email shortly.";
setTimeout("window.close()", 3000);
}
else
window.alert("not valid"); //for debugging
document.getElementById("message").innerHTML = "Please enter a valid email address";
return false; // this stops the form from submitting, and the page from reloading.
}
</script>
</head>
<body id="body">
<p>If you sign up for our newsletter you will be entered into a drawing to win free clothes every month!</p>
<br>
<p id="message"> </p>
<form action="" onsubmit="validateEmail();">
<p>Email: <input type="text" name="email"/>
<input type="submit" value="Sign up"/></p>
<br>
<input type="button" value="No thanks" onclick="window.close()"/>
<br>
</form>
<p><a href="privacy.html">Privacy Policy</a></p>
</body>
</html>
函数validateEmail(){
var emailRule=/^([^()[\]\\,;:\s@\“]+(\.[^()[\]\,;:\s@\“]+)*(\“+\”)((\[[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[1,3}])){124;([a-zA Z-0-9]+-Z]+-[a-2]/];
if(emailRule.test(document.forms[0].email.value)){
document.getElementById(“body”).innerHTML=“感谢您注册我们的时事通讯!您将很快收到一封确认电子邮件。”;
setTimeout(“window.close()”,3000);
}
其他的
window.alert(“无效”);//用于调试
document.getElementById(“message”).innerHTML=“请输入有效的电子邮件地址”;