jQuery效果之封装模拟placeholder效果,让低版本浏览器也支持
页面中的输入框默认的提示文字一般使用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效果,让低版本浏览器也支持的更多相关文章
- 轮播图采用js、jquery实现无缝滚动和非无缝滚动的四种案例实现,兼容ie低版本浏览器
项目源代码下载地址:轮播图 以下为项目实现效果:(由于gif太大,所以只上传一张图片,但效果完全能实现,经测试,在ie各版本浏览器及chrome,firefox等浏览器中均能实现效果,可以实现点击切换 ...
- 在IE8等不支持placeholder属性的浏览器中模拟placeholder效果
placeholder是一个很有用的属性,可以提示用户在input框中输入正确的内容,但是IE8以及IE8一下的浏览器不支持该属性,我们可以使用js来模拟相似的效果.下面直接上代码: <!doc ...
- 解决html5新标签【placeholder】低版本浏览器下不兼容问题
placeholder属性是HTML5 中为input添加的.在input上提供一个占位符,文字形式展示输入字段预期值的提示信息(hint),该字段会在输入为空时显示. 实例:1 <input ...
- 【jquery】基于 jquery 实现 ie 浏览器兼容 placeholder 效果
placeholder 是 html5 新增加的属性,主要提供一种提示(hint),用于描述输入域所期待的值.该提示会在输入字段为空时显示,并会在字段获得焦点时消失.placeholder 属性适用于 ...
- IE下支持文本框和密码框placeholder效果的JQuery插件
基于jQuery实现的,主要用于IE下实现placeholder效果,可同时支持文本和密码输入框.placeholder是HTML5新增的一个属性,当input设置了该属性后,该值的内容将作为灰色提示 ...
- Jquery简单的placeholder效果
Jquery简单的placeholder效果 由于IE6-IE9不支持HTML5中的placeholder,所以自己依赖于Jquery简单的写了一个,供参考! 先看看效果吧!如下JSFiddle地址 ...
- 跨浏览器实现placeholder效果的jQuery插件
曾经遇到这样一个问题,处理IE8密码框placeholder属性兼容性.几经周折,这个方案是可以解决问题的. 1.jsp页面引入js插件 <script type="text/java ...
- 利用jquery模拟select效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jQuery 两种方法实现IE10以下浏览器的placeholder效果
/* ** jQuery版本:jQuery-1.8.3.min.js ** 测试的浏览器:IE8,IETester下的IE6-IE9** Author:博客园小dee */ placeholder是H ...
随机推荐
- Go语言strings和strconv包
目录 前缀和后缀 字符串包含关系 判断子字符串或字符在父字符串中出现的位置(索引) 字符串替换 统计字符串出现次数 重复字符串 修改字符串大小写 修剪字符串 分割字符串 拼接slice到字符串 从字符 ...
- [Swift]创建CoreData的两种方式
一.CoreData介绍 CoreData主要分为两部分: 上层是模型层,模型层有NSManagedObjectContext上下文管理着, 底层则是由SQLite实现的持久化部分,通过NSPersi ...
- JavaScript状态模式及状态机模型
这是一篇,我自己都看不完的文章... 文章大体就两部分: 状态模式的介绍 状态机模型的函数库javascript-state-machine的用法和源码解析 场景及问题背景: 我们平时开发时本质上就是 ...
- 利用 ELK 搭建 Docker 容器化应用日志中心
利用 ELK 搭建 Docker 容器化应用日志中心 概述 应用一旦容器化以后,需要考虑的就是如何采集位于 Docker 容器中的应用程序的打印日志供运维分析.典型的比如SpringBoot应用的日志 ...
- php(curl请求)测试接口案例
请求测试接口,如下: $data = [']; $result = curlrequest($apiUrl,$data); ){ echo json_encode($result); }else{ e ...
- 全网最详细的如何在谷歌浏览器里正确下载并安装Postman【一款功能强大的网页调试与发送网页HTTP请求的Chrome插件】(图文详解)
不多说,直接上干货! 想必,玩过Java Web的你,肯定是对于http post和get等请求测试的过程记忆犹新吧. Postman的安装方法分好几种,主要分为两种安装模式介绍: (1)chrome ...
- [Shell]sed命令在MAC和Linux下的不同使用方式
---------------------------------------------------------------------------------------------------- ...
- Apache XBean相关说明,待补充
前言 最近在看ActiveMQ5.15.0源码,发现ActiveMQ实际上是基于spring实现的,其配置文件activemq.xml中有个broker元素节点,使用的就是Apache XBean的配 ...
- 聊聊JVM(二)说说GC的一些常见概念
转自CSDN 上一篇总结GC的基础算法,各种GC收集器的基本原理,还是比较粗粒度的概念.这篇会整理一些GC的常见概念,理解了这些概念,相信对GC有更加深入的理解 1. 什么时候会触发Minor GC? ...
- redis学习总结-redis作为MyBatis的自定义缓存
1.RedisCache.java package com.houtai.cache; import java.util.concurrent.locks.ReadWriteLock; import ...