如果您担心旧版浏览器,请尝试以下代码:
HTML
<input id="user" type="text" placeholder="someone@example.com" /><br />
<input type="password" placeholder="Password" />
JS
(function($){
var placeholderIsSupported = ('placeholder' in document.createElement('input'));
$.fn.emulatePlaceholder = function(){
if(!placeholderIsSupported){
this.each(function(index, element){
var handle = $(element);
var placeholder = handle.attr('placeholder');
if(handle.val() == ''){
handle.val(placeholder);
}
handle.blur(function(e){
var handle = $(this);
if(handle.val() == ''){
handle.val(placeholder);
}
});
handle.focus(function(e){
var handle = $(this);
if(handle.val() == placeholder){
handle.val('');
}
});
});
}
};
})(jQuery);
用法
$('input').emulatePlaceholder();
jsFiddle 示例。
测试上面的小提琴IE<8
以查看实际工作的代码