从javascript打印或回显html页面中的消息

从javascript打印或回显html页面中的消息,javascript,html,Javascript,Html,我有以下脚本 function validateEmailp() { var messagemail = document.getElementById('emailerrorz').innerText; var two = document.getElementById('email').value; var first = two.split("@")[1]; var badEmails = ["gmail.com", "yahoo.com"] if (badEmails.indexOf(f

我有以下脚本

function validateEmailp() {
var messagemail = document.getElementById('emailerrorz').innerText;
var two = document.getElementById('email').value;
var first = two.split("@")[1];
var badEmails = ["gmail.com", "yahoo.com"]
if (badEmails.indexOf(first) != -1) {
        document.getElementById("email").value = ""; //this works
    messagemail = 'We do not accept free e-mails'; //this doesn't
    return false;
    }   
return true;
}
和HTML

<td>{EMAILFIELD}<span id="emailerrorz"></span></td>
{EMAILFIELD}
而且{EMAILFIELD}是用PHP编写的

<input id="email" class="txt" type="text" name="email" size="25" value="" maxlength="255" onblur="validateEmailp()"></input>


但在打印span id中的错误时,它对我不起作用。它只在重置span id中的值时起作用。

属性不能以这种方式工作。你想要:

 document.getElementById('emailerrorz').innerText = 'We do not accept free e-mails'


当您执行
var messagemail=document.getElementById('emailerrorz').innerText变量存储包含该内容的字符串

当您
var messagemail=document.getElementById('emailerrorz')变量存储对象/元素,然后可以使用属性
。innerText

因此,请使用:

var messagemail = document.getElementById('emailerrorz');
// rest of code
messagemail.innerText = 'We do not accept free e-mails';

@阿德里安:当我点击另一个字段时,除了重置它之外,还应该在电子邮件字段中将错误打印在一边。但从我在小提琴上看到的情况来看,它并不是。它表明你需要添加文本内容而不是内部文本。
var messagemail = document.getElementById('emailerrorz');
// rest of code
messagemail.innerText = 'We do not accept free e-mails';