有人可以帮我吗?我是javaScript的新手,无法使用onblur或onfocus功能。这是HTML代码和.JS代码的片段。我已经在结束的body标签下方和head标签的右侧调用了我的script.js,以确保不是这样的......
HTML 的
<label for="name">Full Name</label>
<input type="text" id="name" name="fullname" value="First Name" />
<label for="phone">Phone Number</label>
<input type="text" id="phone" name="phonenumber" />
的JavaScript
function prepareEventHandlers() {
var firstNameField = document.getElementById("name");
var phoneField = document.getELementsById("phone");
firstNameField.onfocus = function() {
if (firstNameField.value = "First Name") {
firstNameField.value = " ";
}
};
firstNameField.onblur = function() {
if (firstNameField.value == " ") {
firstNameField.value = "First Name";
}
};
}
window.onload = function() {
prepareEventHandlers();
}
答案 0 :(得分:1)
有一个拼写错误:var phoneField = document.getELementsById(“phone”);
答案 1 :(得分:0)
function prepareEventHandlers() {
var firstNameField = document.getElementById("name");
var phoneField = document.getElementById("phone");
firstNameField.onfocus = function() {
if (firstNameField.value == "First Name") {
firstNameField.value = "";
}
};
firstNameField.onblur = function() {
if (!firstNameField.value.length) {
firstNameField.value = "First Name";
}
};
}
window.onload = function() {
prepareEventHandlers();
}