0

有人可以帮我弄这个吗?我是 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();
}
4

2 回答 2

1

有一个错字: var phoneField = document.getELementsById("phone");

于 2013-10-03T14:18:59.043 回答
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();
}
于 2013-10-03T14:49:43.163 回答