在我们写移动端项目时,会发现在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('');
}
});
});
}
本文提供了一种解决iOS设备上部分输入框默认文字隐藏的方法,通过一段JS代码确保placeholder文本在焦点离开且输入框为空时显示,适用于移动端项目。
547

被折叠的 条评论
为什么被折叠?



