即使输入密码的ID存在,我仍然在onfocus函数中出现错误
这是我的代码的片段
let myInput = document.getElementById("pwd");
myInput.onfocus = function () {
document.getElementById("message").style.display = "block";
}
myInput.onblur = function () {
document.getElementById("message").style.display = "none";
}
<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-user"></i>
</span>
<input name="user_password" id="pwd" placeholder="Password" class="form-control" type="password">
</div>
</div>
<div id="message">
....
</div>
</div>
答案 0 :(得分:1)
您的代码的问题是"message"
的ID不存在,因此您发现此错误。
let myInput = document.getElementById("pwd");
myInput.onfocus = function() {
document.getElementById("message").style.display = "block";
}
myInput.onblur = function() {
document.getElementById("message").style.display = "none";
}
<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-user"></i>
</span>
<input name="user_password" id="pwd" placeholder="Password" class="form-control" type="password">
</div>
</div>
<div id="message" style="display:none">Message goes here</div>
答案 1 :(得分:0)
在您将div
添加到id=message
后,您的代码似乎运行正常
let myInput = document.getElementById("pwd");
myInput.onfocus = function () {
document.getElementById("message").style.display = "block";
}
myInput.onblur = function () {
document.getElementById("message").style.display = "none";
}
<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-user"></i>
</span>
<input name="user_password" id="pwd" placeholder="Password" class="form-control" type="password">
</div>
</div>
<div id="message">
....
</div>
</div>