document.getElementsById(" ...")。innerHTML不会改变h2标签

时间:2017-02-25 15:28:27

标签: javascript php html

var types = [ { name: 'Type0', displayKey: 'Type 0' }, { name: 'Type1', displayKey: 'Type 1' } ] var args = types.map(function (type, i) { return { name: type.name, displayKey: type.displayKey, source: bloodHound[i].ttAdapter(), templates: { header: '<h4 class="typeahead-header"><img src="' + type.name + '.png">' + type.name + '</h4>', suggestion: Handlebars.compile(["{{name}}<br><small>{{address}} </small>"].join("")) } } }) args.unshift({ minLength: 3 }) // Calls typeahead with a variable number of arguments on the search box $.fn.typeahead.apply($("#searchbox"), args) 代码后document.getElementsById("occ").innerHTML = "Already registered";没有变更,但页面看起来像重新加载。 代码:

<h2 style="color: red" id ="occ"></h2>

你知道为什么<!doctype html> <html> <head> <meta charset="utf-8"> <title>Ustory - Register</title> <link href="https://fonts.googleapis.com/css?family=Raleway:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet"> <link href="CSS_defaults.css" rel="stylesheet"> <script src="Ustory_JS.js"></script> </head> <body> <?php if($_GET['occupied'] == "true") { echo($_GET['occupied']); $occupied = "true"; } ?> <script> var occ = <?php echo json_encode($occupied); ?>; alert(typeof(occ)); if(occ == "true") { alert("done"); document.getElementsById("occ").innerHTML = "Already registered"; } </script> <div id="reg_in" class="reg" style="color:#FF4D50;"> <form class="reg" name="register" action="U_story_reg.php" method="post" onsubmit="return register_()"> <input type="text" name="firstname" id="firstname" placeholder="Meno" required> <b>*</b><br> <input type="text" name="lastname" id="lastname" placeholder="Priezvisko" required> <b>*</b><br> <input type="text" name="nick" id="nick" placeholder="Prezívka">&nbsp;&nbsp;&nbsp;<br> <input type="email" name="mail" id="mail" placeholder="E-mail" required> <b>*</b><br> <input type="password" name="pass" id="pass" placeholder="Heslo" required> <b>*</b><br> <input type="password" name="pass_again" id="pass_again" placeholder="Heslo znovu" required> <b>*</b><br> <input type="submit" value="Submit"> <h2 style="color: red" id ="occ"></h2> </form> </div> <h2 style="color: red" id ="occ"></h2> </body> </html> 不起作用吗?

2 个答案:

答案 0 :(得分:1)

代码在DOM元素加载之前执行&amp;你有一个错误的getElementsById应该是getElementById。您必须将代码移动到您尝试更改的html元素下面,或将其包装在student_date_ended函数中。像这样:

$(document).ready

答案 1 :(得分:0)

此外,请确保引用jQuery库以使用document.ready()方法。请查看以下链接以获取更多信息.. https://www.w3schools.com/jquery/event_ready.asp