【发布时间】:2012-07-03 11:04:37
【问题描述】:
我正在尝试在我的 Web 应用程序中使用 placeholder="xxx" 属性,但我不想为 IE9 提供特殊的视觉效果。人们能否提出一些在 IE9 中实现此功能的好建议?
我在这里找到了几个链接,但建议的脚本都不够用……答案是从 2011 年年中开始的,所以我想也许有更好的解决方案。也许有一个被广泛采用的 jQuery 插件?我不想使用任何需要侵入性代码的东西,例如需要某个 css 类或其他东西。
谢谢。
EDIT - 我也需要这个来处理密码输入字段。
// 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();
});
}
}
}
【问题讨论】:
-
placeholder in ie9 的可能重复项
标签: html internet-explorer-9 placeholder