JQuery输入框获取/失去焦点行为

//搜索框获取焦点清除内容
$(function() {
    $("input").focus(function() { //获取焦点,清空默认内容
        $(this).css('color', 'black').addClass('focus');
        if ($(this).val() == this.defaultValue) {
            $(this).val('');
        }
    }).blur(function() { //失去焦点,若没有内容,则恢复默认内容;有内容则不变
        $(this).removeClass('focus');
        if ($(this).val() == '') {
            $(this).val(this.defaultValue).css('color', '#aaa');
        }
    });
});

注:.focus只是输入框的css效果,可以忽略

原文地址:https://www.cnblogs.com/3body/p/5416369.html