This question already has answers here:
'innerText' works in IE, but not in Firefox
                            
                                (15个答案)
                            
                    
                5年前关闭。
        

    

我有以下JavaScript代码,用于验证登录表单,并提交该表单是否有效。该代码适用于chrome和IE。
但是在Firefox上,它不会在passwordinfousernameinfodocument.getElementById("passwordinfo").innerText = "please enter password";)上显示错误反馈
请帮我发现问题,因为我要添加Ajax,这使我感到恐惧。

<!DOCTYPE html>
<html>
    <head>
    <link href="mystyle.css" rel="stylesheet" type="text/css">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
    <title>welcome</title>
    <script type="text/javascript">
    function isvalidmail(mail) {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return filter.test(mail);
    }

    function checklogin() {
        var email = document.getElementById("username").value;
        var password = document.getElementById("password").value;
        if(email.length < 1 || email.length > 30||(!isvalidmail(email)))
            document.getElementById("usernameinfo").innerText = "please enter valid email";
        else {
            document.getElementById("usernameinfo").innerText = "";
            if(password.length === 0)
                document.getElementById("passwordinfo").innerText = "please enter password";
            else {
                document.getElementById("passwordinfo").innerText = "";
                document.forms["login"].submit();
            }
        }
    }
    </script>
    </head>
    <body onload="checkCookies()">
        <script>
            function checkCookies(){
                if (!navigator.cookieEnabled==true){
                    document.write("You need to enable cookies to make the site work properly.<br>Please          enable cookies and <a href='index.php'>Reload</a>");
                }
            }
        </script>
        <div id="container">
            <div id="headerpart">
                <?php include "module_header.php" ?>
            </div>
            <div id="bodypart">
                <div id="nav">
                    <?php
                    include 'navigation_bar.php';
                    ?>
                </div>
                <div id="mainop">
                    <span id="sessionexp"><?php if(isset($_GET["redirect"]))if($_GET["redirect"]=='session') echo         "Your Session has Expired please login"; ?></span>
                    <form id="login" action="checklogin.php" method="POST" align="center">
                        <table border="0"  bgcolor="#FFFFCB" align="center"><br/>
                            <thead>please login below</thead>
                            <tr>
                                <td>email:</td>
                                <td><input type="text" id="username" name="username"  /></td>
                                <td>
                                    <span class="warning" id="usernameinfo" name="usernameinfo">
                                        <?php
                                        if(isset($_GET["errno"]) && $_GET["errno"] == 1)
                                        echo "wrong username."
                                        ?>
                                    </span>
                                </td>
                            </tr>
                            <tr>
                                <td>password:</td>
                                <td><input type="password" id="password" name="password" /></td>
                                <td>
                                    <span class="warning" id="passwordinfo" name="passwordinfo">
                                        <?php
                                        if(isset($_GET["errno"]) && $_GET["errno"] == 2)
                                        echo "wrong password.";
                                        ?>
                                    </span>
                                </td>
                            </tr>
                        </table>
                        <br />

                        <input type="button" value="Login" onclick="checklogin()"/>
                        <?php echo " New User?  <a href=\"register.php\"><b>Register freely!</b></a>"; ?>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

最佳答案

采用,

document.getElementById('passwordinfo').textContent=''; //For firefox

或尝试

document.getElementById('passwordinfo').innerHTML='';


有关更多信息,请单击HERE

09-25 16:36
查看更多