页面中的输入框默认的提示文字一般使用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 实现秒级弹性伸缩的方法
# 输入框
# 不需要
# 不支持
# 给大家
# 链式
# 绑定
# 为空
# 并让
# 版本浏览器
# 最多
# 也要
# 要在
# 希望能
# 请输入
# 这篇
# 来实现
# 小编
# 大家多多
# 就可以
# 先写
相关文章:
如何选择靠谱的建站公司加盟品牌?
如何快速打造个性化非模板自助建站?
如何在万网ECS上快速搭建专属网站?
网站制作公司排行榜,抖音怎样做个人官方网站
唐山网站制作公司有哪些,唐山找工作哪个网站最靠谱?
头像制作网站在线制作软件,dw网页背景图像怎么设置?
如何用好域名打造高点击率的自主建站?
建站主机与虚拟主机有何区别?如何选择最优方案?
建站主机类型有哪些?如何正确选型
武汉外贸网站制作公司,现在武汉外贸前景怎么样啊?
建站之星导航如何优化提升用户体验?
宝塔Windows建站如何避免显示默认IIS页面?
建站主机服务器选购指南:轻量应用与VPS配置解析
建站主机默认首页配置指南:核心功能与访问路径优化
简历在线制作网站免费,免费下载个人简历的网站是哪些?
网页制作模板网站推荐,网页设计海报之类的素材哪里好?
建站之星北京办公室:智能建站系统与小程序生成方案解析
如何访问已购建站主机并解决登录问题?
郑州企业网站制作公司,郑州招聘网站有哪些?
如何使用Golang安装API文档生成工具_快速生成接口文档
如何制作网站标识牌,动态网站如何制作(教程)?
建站主机选择指南:服务器配置与SEO优化实战技巧
开封网站制作公司,网络用语开封是什么意思?
济南企业网站制作公司,济南社保单位网上缴费步骤?
,sp开头的版面叫什么?
建站之星后台密码如何安全设置与找回?
如何在建站宝盒中设置产品搜索功能?
如何正确下载安装西数主机建站助手?
ui设计制作网站有哪些,手机UI设计网址吗?
深入理解Android中的xmlns:tools属性
C++如何使用std::optional?(处理可选值)
网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?
微信小程序制作网站有哪些,微信小程序需要做网站吗?
宁波免费建站如何选择可靠模板与平台?
PHP 500报错的快速解决方法
深圳网站制作平台,深圳市做网站好的公司有哪些?
电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?
网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?
家具网站制作软件,家具厂怎么跑业务?
C#怎么创建控制台应用 C# Console App项目创建方法
高端企业智能建站程序:SEO优化与响应式模板定制开发
怀化网站制作公司,怀化新生儿上户网上办理流程?
网站设计制作公司地址,网站建设比较好的公司都有哪些?
南阳网站制作公司推荐,小学电子版试卷去哪里找资源好?
如何高效完成自助建站业务培训?
如何在云虚拟主机上快速搭建个人网站?
如何选择美橙互联多站合一建站方案?
微信网站制作公司有哪些,民生银行办理公司开户怎么在微信网页上查询进度?
建站主机选哪种环境更利于SEO优化?
武汉网站设计制作公司,武汉有哪些比较大的同城网站或论坛,就是里面都是武汉人的?
*请认真填写需求信息,我们会在24小时内与您取得联系。