我正在尝试placeholder="xxx"在我的Web应用程序中使用该属性,我不想为IE9提供特殊的视觉效果.人们可以在IE9中为实现此功能提出一些好的建议吗?
我在这里找到了几个链接,但没有一个建议的脚本足够......答案是从2011年中期开始的,所以我想也许有更好的解决方案.也许有广泛采用的jQuery插件?我不想使用任何需要侵入式代码的东西,例如需要某个css类或其他东西.
谢谢.
编辑 - 我还需要这个用于密码输入字段.
// the below snippet should work, but isn't.
$(document).ready(function() {
initPlaceholders()();
}
function initPlaceholders() {
$.support.placeholder = false;
var test = document.createElement('input');
if ('placeholder' in test) {
$.support.placeholder = true;
return function() { }
} else {
return function() {
$(function() {
var active = document.activeElement;
$('form').delegate(':text, :password', 'focus', function() {
var _placeholder = $(this).attr('placeholder'),
_val = $(this).val();
if (_placeholder != '' && _val == _placeholder) {
$(this).val('').removeClass('hasPlaceholder');
}
}).delegate(':text, :password', 'blur', function() {
var _placeholder = $(this).attr('placeholder'),
_val = $(this).val();
if (_placeholder != '' && (_val == '' || _val == _placeholder)) {
$(this).val(_placeholder).addClass('hasPlaceholder');
}
}).submit(function() {
$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
});
$(':text, :password').blur();
$(active).focus();
});
}
}
}