页面中的输入框默认的提示文字一般使用placeholder属性就可以了,即:

<input type="text" name="username" placeholder="请输入用户名" value="" id="username"/>

最多加点样式控制下默认文字的颜色

input::-webkit-input-placeholder{color:#AAAAAA;}

但是在低版本的浏览器却不支持这个placeholder属性,那么真的要在低版本浏览器也要实现跟placeholder一样的效果,就需要写个插件来兼容下,下面就细讲一下怎样用jquery来实现这个模拟效果。

实现这个模拟效果,页面的一般调用方式:

$('input').placeholder();

首先,先写jquery插件的一般结构:

;(function($){
$.fn.placeholder = function(){
//实现placeholder的代码
}
})(jQuery)

下面我们就要判断浏览器是否支持placeholder属性

;(function($){
$.fn.placeholder = function(){ this.each(function(){
var _this = this;
var supportPlaceholder = 'placeholder' in document.createElement('input');
if(!supportPlaceholder){
//不支持placeholder属性的操作 }
});
}
})(jQuery)

我们要支持链式操作,如下:

;(function($){
$.fn.placeholder = function(){ return this.each(function(){
var _this = this;
var supportPlaceholder = 'placeholder' in document.createElement('input');
if(!supportPlaceholder){
//不支持placeholder属性的操作 }
});
}
})(jQuery)

默认配置项:

options = $.extend({
placeholderColor:'#aaaaaa',
isSpan:false, //是否使用插入span标签模拟placeholder的方式,默认是不需要
onInput:true //实时监听输入框
},options);

如果不需要通过span来模拟placeholder效果,那么就需要通过输入框的value值来判断,如下代码:

if(!options.isSpan){
$(_this).focus(function () {
var pattern = new RegExp("^" + defaultValue + "$|^$");
pattern.test($(_this).val()) && $(_this).val('').css('color', defaultColor);
}).blur(function () {
if($(_this).val() == defaultValue) {
$(_this).css('color', defaultColor);
}
else if($(_this).val().length == 0) {
$(_this).val(defaultValue).css('color', options.placeholderColor)
}
}).trigger('blur');
}

如果需要同span标签来模拟placeholder效果,代码如下:

var $simulationSpan = $('<span class="wrap-placeholder">'+defaultValue+'</span>');
$simulationSpan.css({
'position':'absolute',
'display':'inline-block',
'overflow':'hidden',
'width':$(_this).outerWidth(),
'height':$(_this).outerHeight(),
'color':options.placeholderColor,
'margin-left':$(_this).css('margin-left'),
'margin-top':$(_this).css('margin-top'),
'padding-left':parseInt($(_this).css('padding-left')) + 2 + 'px',
'padding-top':_this.nodeName.toLowerCase() == 'textarea' ? parseInt($(_this).css('padding-top')) + 2 : 0,
'line-height':_this.nodeName.toLowerCase() == 'textarea' ? $(_this).css('line-weight') : $(_this).outerHeight() + 'px',
'font-size':$(_this).css('font-size'),
'font-family':$(_this).css('font-family'),
'font-weight':$(_this).css('font-weight')
}); //通过before把当前$simulationSpan添加到$(_this)前面,并让$(_this)聚焦
$(_this).before($simulationSpan.click(function () {
$(_this).trigger('focus');
})); //当前输入框聚焦文本内容不为空时,模拟span隐藏
$(_this).val().length != 0 && $simulationSpan.hide(); if (options.onInput) {
//绑定oninput/onpropertychange事件
var inputChangeEvent = typeof(_this.oninput) == 'object' ? 'input' : 'propertychange';
$(_this).bind(inputChangeEvent, function () {
$simulationSpan[0].style.display = $(_this).val().length != 0 ? 'none' : 'inline-block';
});
}else {
$(_this).focus(function () {
$simulationSpan.hide();
}).blur(function () {
/^$/.test($(_this).val()) && $simulationSpan.show();
});
};

整体代码:

;(function($){
$.fn.placeholder = function(options){
options = $.extend({
placeholderColor:'#aaaaaa',
isSpan:false, //是否使用插入span标签模拟placeholder的方式,默认是不需要
onInput:true //实时监听输入框
},options); return this.each(function(){
var _this = this;
var supportPlaceholder = 'placeholder' in document.createElement('input');
if(!supportPlaceholder){
//不支持placeholder属性的操作
var defaultValue = $(_this).attr('placeholder');
var defaultColor = $(_this).css('color');
if(!options.isSpan){
$(_this).focus(function () {
var pattern = new RegExp("^" + defaultValue + "$|^$");
pattern.test($(_this).val()) && $(_this).val('').css('color', defaultColor);
}).blur(function () {
if($(_this).val() == defaultValue) {
$(_this).css('color', defaultColor);
}
else if($(_this).val().length == 0) {
$(_this).val(defaultValue).css('color', options.placeholderColor)
}
}).trigger('blur');
}else{
var $simulationSpan = $('<span class="wrap-placeholder">'+defaultValue+'</span>');
$simulationSpan.css({
'position':'absolute',
'display':'inline-block',
'overflow':'hidden',
'width':$(_this).outerWidth(),
'height':$(_this).outerHeight(),
'color':options.placeholderColor,
'margin-left':$(_this).css('margin-left'),
'margin-top':$(_this).css('margin-top'),
'padding-left':parseInt($(_this).css('padding-left')) + 2 + 'px',
'padding-top':_this.nodeName.toLowerCase() == 'textarea' ? parseInt($(_this).css('padding-top')) + 2 : 0,
'line-height':_this.nodeName.toLowerCase() == 'textarea' ? $(_this).css('line-weight') : $(_this).outerHeight() + 'px',
'font-size':$(_this).css('font-size'),
'font-family':$(_this).css('font-family'),
'font-weight':$(_this).css('font-weight')
}); //通过before把当前$simulationSpan添加到$(_this)前面,并让$(_this)聚焦
$(_this).before($simulationSpan.click(function () {
$(_this).trigger('focus');
})); //当前输入框聚焦文本内容不为空时,模拟span隐藏
$(_this).val().length != 0 && $simulationSpan.hide(); if (options.onInput) {
//绑定oninput/onpropertychange事件
var inputChangeEvent = typeof(_this.oninput) == 'object' ? 'input' : 'propertychange';
$(_this).bind(inputChangeEvent, function () {
$simulationSpan[0].style.display = $(_this).val().length != 0 ? 'none' : 'inline-block';
});
}else {
$(_this).focus(function () {
$simulationSpan.hide();
}).blur(function () {
/^$/.test($(_this).val()) && $simulationSpan.show();
});
};
}
}
});
}
})(jQuery);

调用方式,需要通过span标签来模拟的话:

$("#username").placeholder({
isSpan:true
});

jQuery效果之封装模拟placeholder效果,让低版本浏览器也支持的更多相关文章

  1. 轮播图采用js、jquery实现无缝滚动和非无缝滚动的四种案例实现,兼容ie低版本浏览器

    项目源代码下载地址:轮播图 以下为项目实现效果:(由于gif太大,所以只上传一张图片,但效果完全能实现,经测试,在ie各版本浏览器及chrome,firefox等浏览器中均能实现效果,可以实现点击切换 ...

  2. 在IE8等不支持placeholder属性的浏览器中模拟placeholder效果

    placeholder是一个很有用的属性,可以提示用户在input框中输入正确的内容,但是IE8以及IE8一下的浏览器不支持该属性,我们可以使用js来模拟相似的效果.下面直接上代码: <!doc ...

  3. 解决html5新标签【placeholder】低版本浏览器下不兼容问题

    placeholder属性是HTML5 中为input添加的.在input上提供一个占位符,文字形式展示输入字段预期值的提示信息(hint),该字段会在输入为空时显示. 实例:1 <input ...

  4. 【jquery】基于 jquery 实现 ie 浏览器兼容 placeholder 效果

    placeholder 是 html5 新增加的属性,主要提供一种提示(hint),用于描述输入域所期待的值.该提示会在输入字段为空时显示,并会在字段获得焦点时消失.placeholder 属性适用于 ...

  5. IE下支持文本框和密码框placeholder效果的JQuery插件

    基于jQuery实现的,主要用于IE下实现placeholder效果,可同时支持文本和密码输入框.placeholder是HTML5新增的一个属性,当input设置了该属性后,该值的内容将作为灰色提示 ...

  6. Jquery简单的placeholder效果

    Jquery简单的placeholder效果 由于IE6-IE9不支持HTML5中的placeholder,所以自己依赖于Jquery简单的写了一个,供参考! 先看看效果吧!如下JSFiddle地址 ...

  7. 跨浏览器实现placeholder效果的jQuery插件

    曾经遇到这样一个问题,处理IE8密码框placeholder属性兼容性.几经周折,这个方案是可以解决问题的. 1.jsp页面引入js插件 <script type="text/java ...

  8. 利用jquery模拟select效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. jQuery 两种方法实现IE10以下浏览器的placeholder效果

    /* ** jQuery版本:jQuery-1.8.3.min.js ** 测试的浏览器:IE8,IETester下的IE6-IE9** Author:博客园小dee */ placeholder是H ...

随机推荐

  1. Go语言map

    map 是一种特殊的数据结构:一种元素对(pair)的无序集合,pair 的一个元素是 key,对应的另一个元素是 value,所以这个结构也称为关联数组或字典.这是一种快速寻找值的理想结构:给定 k ...

  2. document.getElementById 和 document.getElementsByClassName获取DOM元素的区别

    想必小伙伴们对于 JS 获取DOM的几种方法早已烂熟于心,了然于胸,   尤其是 document.getElementById 和 document.getElementsByClassName, ...

  3. ueditor后台配置项返回格式出错,上传功能将不能正常使用

    和https://ask.csdn.net/questions/382087问题一样. java+jsp1.config.json配置不对2.百度依赖的jar包没引入3.请求controller.js ...

  4. Nginx 搭建图片缓存服务器-转

    文章:https://waver.me/2019/04/11/Nginx-Cache-Server/ 参考: Nginx 配置详解Nginx 简易教程Nginx 配置总结

  5. 项目总结一:情感分类项目(emojify)

    一.Emojifier-V1 模型 1. 模型 (1)前向传播过程: (2)损失函数:计算the cross-entropy cost (3)反向传播过程:计算dW,db dz = a - Y_oh[ ...

  6. linux取IP的几个方法

    ifconfig eth0|grep " inet add"|cut -d":" -f2|cut -d " " -f1 ifconfig e ...

  7. 课程回顾-Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization

    训练.验证.测试划分的量要保证数据来自一个分布偏差方差分析如果存在high bias如果存在high variance正则化正则化减少过拟合的intuitionDropoutdropout分析其它正则 ...

  8. Mac下快速搭建PHP开发环境

    最近做了一个后端的项目,是用PHP+MySQL+Nginx做的,所以把搭建环境的方法简单总结一下. 备注: 物料:Apache/Nginx+PHP+MySQL+MAMP Mac OS 10.12.1 ...

  9. 【Android基础】Fragment 详解之Fragment生命周期

    上一篇文章简单介绍了一下Fragment,这一篇文章会详细的说一下Fragment的生命周期和创建一个用户界面. Fragment的主要功能就是创建一个View,并且有一个生命周期来管理这个View的 ...

  10. web进修之—Hibernate HQL(7)

    概述 HQL是Hibernate封装成为面向对象的数据库查询语言,具有如下特点: 面向对象,包括继承.多态和关联之类的概念,SQL操作的数据库的表,HQL更像是操作对象 大小写敏感,只对对象和属性敏感 ...