简体   繁体   English

如何通过javascript更改textContent?

[英]How to change textContent via javascript?

I have a registration form page, and there is empty div i want to use to display errors. 我有一个注册表单页面,并且我想使用空div来显示错误。 To check forms before triggering php script i use javascript: 要在触发php脚本之前检查表单我使用javascript:

function errorHandler(){
    var loginIn;
    var passIn;


    loginIn = document.forms["regForm"]["login"].value;
    passIn = document.forms["regForm"]["password"].value;

    if (loginIn == "" || loginIn == null) {
        alert("LOGIN CANNOT BE EMPTY");
        return false;
    } 
}


It works fine, and alert message do appear when i call them like this: 它工作正常,当我这样称呼它时会出现警报消息:

<form name="regForm" action= "save_user.php" onsubmit="return errorHandler()" method="post"> . <form name="regForm" action= "save_user.php" onsubmit="return errorHandler()" method="post">

But there is as i mentioned before a div inside of the form: 但正如我之前提到的形式中的div:

div id ="errorArea"></div> and when i try to put a text inside of this div like this: div id ="errorArea"></div>当我尝试将文本放入此div中时,如下所示:

function errorHandler(){

    var loginIn;
    var passIn;
    var erorAreaMessage;

    loginIn = document.forms["regForm"]["login"].value;
    passIn = document.forms["regForm"]["password"].value;
    erorAreaMessage = document.getElementById('errorArea').textContent;

    if (loginIn == "" || loginIn == null) {
        erorAreaMessage = "LOGIN CANNOT BE EMPTY";
        return false;
    }
}

Nothing happens, can someone explain me why? 什么都没发生,有人能解释我为什么吗?

You need to put the value inside the <div> . 您需要将值放在<div> That can done by setting the innerHTML or textContent property to your error-message. 这可以通过将innerHTMLtextContent属性设置为错误消息来完成。 Try this - 尝试这个 -

...
if (loginIn == "" || loginIn == null) {
    document.getElementById('errorArea').textContent = "LOGIN CANNOT BE EMPTY";
    return false;
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM