简体   繁体   中英

Beginner Help onblur on focus troubleshooting

Can someone help me with this? I'm new to javaScript and cannot get my onblur or onfocus functionality to work. Here is a snippet of the HTML code and .JS code. I had called my script.js in the right below the ending body tag and in the head tag to ensure that wasn't the case...

                <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" />

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();
}

其中有一个错字:var phoneField = document.getELementsById(“ phone”);

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM