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 ...
随机推荐
- ajax jsonp请求报错not a function的解决方案
概述 最近工作中使用ajax,有时会报json4 is not a function的错误,有时又不会报错.找了很久,网上说是因为多次请求同一个资源导致的,但是我检查了自己的代码,对于重复资源并没有重 ...
- Dash by Plotly 学习笔记
一.介绍 1.dash 是什么 dash 是一个基于 Flask (Python) + React 的 web 框架. 入门指南:https://dash.plot.ly/getting-starte ...
- MySQL 非空约束位置不同对自增列造成的影响
MySQL版本 select version(); +------------+ | version() | +------------+ | 5.7.21-log | +------------+ ...
- 《CLR Via C#》读书笔记:24.运行时序列化
一.什么是运行时序列化 序列化的作用就是将对象图(特定时间点的对象连接图)转换为字节流,这样这些对象图就可以在文件系统/网络进行传输. 二.序列化/反序列化快速入门 一般来说我们通过 FCL 提供的 ...
- Linux编程 12 (默认shell环境变量, PATH变量重要讲解)
一 .概述 默认情况下, bash shell会用一些特定的环境变量来定义系统的环境.这些默认环境变量可以理解是上篇所讲的系统全局环境变量. 1.1 bash shell支持的Bourne变量 Bo ...
- 解决app安装成功后,直接点击“打开”再按home返回,再次打开app会重新启动的问题
在主activity的onCreate中加入以下代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCr ...
- SpingBoot 属性加载
属性加载顺序 配置属性加载的顺序 开发者工具 `Devtools` 全局配置参数: 单元测试上的 `@TestPropertySource` 注解指定的参数: 单元测试上的 `@SpringBootT ...
- leetcode — search-for-a-range
import java.util.Arrays; /** * Source : https://oj.leetcode.com/problems/search-for-a-range/ * * Cre ...
- ES6 系列之私有变量的实现
前言 在阅读 <ECMAScript 6 入门>的时候,零散的看到有私有变量的实现,所以在此总结一篇. 1. 约定 实现 class Example { constructor() { t ...
- spring boot面试问题集锦
译文作者:david 原文链接:https://www.javainuse.com/spring/SpringBootInterviewQuestions Q: 什么是spring boot? A: ...