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

时间:2013-10-04 08:48:13

标签: 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(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}在PHP中

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

但是对于我打印span id中的错误它不起作用。它只适用于从那里重置值。

2 个答案:

答案 0 :(得分:1)

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

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

  var messagemail = document.getElementById('emailerrorz');
  ....
  messagemail.innerText = etc

http://jsfiddle.net/MJXEg/

答案 1 :(得分:1)

执行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';