在我们写移动端项目时,会发现在IOS手机上,部分输入框的默认文字会隐藏,在此提供一套解决方法,此方法已经历过实际项目的测试,可放心的应用于实战
将此段js代码放置在项目的公共js文件中即可
if (!('placeholder' in document.createElement('input'))) {
$('input[placeholder],textarea[placeholder]').each(function () {
var that = $(this),
text = that.attr('placeholder');
if (that.val() === "") {
that.val(text).addClass('placeholder');
}
that.focus(function () {
if (that.val() === text) {
that.val("").removeClass('placeholder');
}
}).blur(function () {
if (that.val() === "") {
that.val(text).addClass('placeholder');
}
}).closest('form').submit(function () {
if (that.val() === text) {
that.val('');
}
});
});
}