2016-10-31 58 views
0

這僅僅是一個部分代碼:如何更改IMG與文本的innerHTML

<div id="login"> 
    <form id="form" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>"> 
     <h1 id="heade">Register!</h1> 
     <input type="text" id="username" name="username" placeholder="username (6-55char)" class="reqi"> 
     <input type="password" id="password" name="password" placeholder="password (6-55char)" class="reqi"> 
     <input type="password" id="passwordconfirmation" name="passwordconfirmation" placeholder="confirm password" class="reqi"> 
     <input type="text" id="email" name="email" placeholder="e-mail adress" class="reqi"> 
     <p id="req">Enter the Captcha code:</p> 
     <h1 id="captcha"><img src="http://i.imgur.com/QgoRq4h.gif" alt="loading"></h1> 
     <input type="text" id="code" name="code"><br> 
     <div id="buttons"> 
      <input type="submit" id="submit"> 
      <button type="button" onclick="login()">Back to Log In</button> 
     </div> 
     <p><?php echo $txt; ?></p> 
    </form> 
</div> 
<script> 
    var captcha =<?php echo $_SESSION['final']; ?>; 
    setTimeout(function() { 
     document.getElementById("captcha").innerHTML = captcha; 
    }, 3000); 
    function login() { 
     window.location = "main.php"; 
    } 
</script> 

等待3秒鐘,頁面不將圖像從'ID =「驗證碼的H1變更後「與可變的驗證碼,它不是空的(我檢查)。我需要一個JavaScript解決方案,因爲我還不知道jQuery。先謝謝你!

回答

0

它不是完整的代碼片段。什麼是$ _SESSION ['final']? 可能你在控制檯中得到了javascript錯誤。

這條線:

var captcha=<?php echo $_SESSION['final']; ?>; 

可生產語法錯誤,如:

var captcha=XXXX; 

嘗試將其更改爲:

var captcha="<?php echo $_SESSION['final']; ?>"; 
0
setTimeout(function() { 
    var e = document.getElementsById('captcha'); 
    var d = document.createElement('span'); 

    d.textContent = captcha; 

    e.appendChild(d); 
    e.removeChild(e); 
}, 3000);