在我们写移动端项目时, 会发现在 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('');
- }
- });
- });
- }
来源: http://www.qdfuns.com/article/33609/6f6d5dd18aa8336720670ce457a830d9.html