我正在使用 html 5 中提供的占位符属性来获得占位符效果。
<input id="search" class="hasPlaceholder" type="text" placeholder="Personal Trainer"
value="" name="search">
我还添加了代码来清除提交时的占位符。
$(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$(':text').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder'));
$(this).addClass('hasPlaceholder');
}
});
$(':text').blur();
$(active).focus();
$('form').submit(function () {
$(this).find('.hasPlaceholder').each(function() {
var input = $(this);
input.removeClass('hasPlaceholder');
if (input.val() == input.attr('placeholder')) {
input.removeAttr('placeholder');
input.val("");
}
});
});
}
});
但它仍然在提交后给出占位符值。