页面中的输入框默认的提示文字一般使用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效果实现方法,让低版本浏览器支持该效果就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
# jquery封装placeholder
# OpenStack Heat AutoScaling详解及实例代码
# jQuery实现IE输入框完成placeholder标签功能的方法
# 使用 tke-autoscaling-placeholder 实现秒级弹性伸缩的方法
# 输入框
# 不需要
# 不支持
# 给大家
# 链式
# 绑定
# 为空
# 并让
# 版本浏览器
# 最多
# 也要
# 要在
# 希望能
# 请输入
# 这篇
# 来实现
# 小编
# 大家多多
# 就可以
# 先写
相关文章:
制作网站的网址是什么,请问后缀为.com和.com.cn还有.cn的这三种网站是分别是什么类型的网站?
如何登录建站主机?访问步骤全解析
宿州网站制作公司兴策,安徽省低保查询网站?
h5在线制作网站电脑版下载,h5网页制作软件?
高性能网站服务器配置指南:安全稳定与高效建站核心方案
如何用好域名打造高点击率的自主建站?
新网站制作渠道有哪些,跪求一个无线渠道比较强的小说网站,我要发表小说?
整蛊网站制作软件,手机不停的收到各种网站的验证码短信,是手机病毒还是人为恶搞?有这种手机病毒吗?
存储型VPS适合搭建中小型网站吗?
简易网站制作视频教程,使用记事本编写一个简单的网页html文件?
宝塔建站教程:一键部署配置流程与SEO优化实战指南
建站之星备案是否影响网站上线时间?
如何在云主机上快速搭建多站点网站?
php json中文编码为null的解决办法
免费网站制作模板下载,除了易企秀之外还有什么H5平台可以制作H5长页面,最好是免费的?
如何选择适合PHP云建站的开源框架?
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
杭州银行网站设计制作流程,杭州银行怎么开通认证方式?
制作网站哪家好,cc、.co、.cm哪个域名更适合做网站?
建站之星如何实现网站加密操作?
宝塔面板创建网站无法访问?如何快速排查修复?
佛山企业网站制作公司有哪些,沟通100网上服务官网?
如何快速生成凡客建站的专业级图册?
网站制作需要会哪些技术,建立一个网站要花费多少?
nginx修改上传文件大小限制的方法
长沙做网站要多少钱,长沙国安网络怎么样?
魔毅自助建站系统:模板定制与SEO优化一键生成指南
宝盒自助建站智能生成技巧:SEO优化与关键词设置指南
公司网站制作费用多少,为公司建立一个网站需要哪些费用?
建站之星安装提示数据库无法连接如何解决?
如何在搬瓦工VPS快速搭建网站?
矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?
如何快速重置建站主机并恢复默认配置?
建站10G流量真的够用吗?如何应对访问高峰?
建站之星导航菜单设置与功能模块配置全攻略
韩国服务器如何优化跨境访问实现高效连接?
建站之星CMS建站配置指南:模板选择与SEO优化技巧
,南京靠谱的征婚网站?
建站主机数据库如何配置才能提升网站性能?
网站建设设计制作营销公司南阳,如何策划设计和建设网站?
建站之星展会模版如何一键下载生成?
电商网站制作公司有哪些,1688网是什么意思?
微信网站制作公司有哪些,民生银行办理公司开户怎么在微信网页上查询进度?
如何在服务器上配置二级域名建站?
如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?
魔方云NAT建站如何实现端口转发?
如何在香港服务器上快速搭建免备案网站?
Android使用GridView实现日历的简单功能
如何通过西部建站助手安装IIS服务器?
建站之星后台管理:高效配置与模板优化提升用户体验
*请认真填写需求信息,我们会在24小时内与您取得联系。