jQuery自定义组件——输入框设置默认提示文字
if (window.jQuery || window.Zepto) { /**
* 设置输入框提示信息
* @param options
* @author rubekid
*/
var setInputTipValue = window.setInputTipValue = function(target, options){
options = options ||{};
var _self = target.get(0);
if(_self._initTip){
return false;
}
target.show();
var color = options.color||"#999999";
var inputPrompt = target.clone();
var name = target.attr("name");
var tipText = options.tipText || target.attr("data-tip") ||"";
if(inputPrompt.is(":input") && inputPrompt.attr("type") != "text"){
inputPrompt = $('<input type="text" />');
} inputPrompt.val(tipText);
inputPrompt.removeAttr("id");
inputPrompt.removeAttr("name");
inputPrompt.removeAttr("maxlength");
inputPrompt.removeAttr("data-tip");
inputPrompt.attr("class", target.attr("class"));
inputPrompt.attr("readonly", true);
inputPrompt.css({"color":color});
_self._initTip = true;//设置标示,防止重复绑定 var _event = "focus";
if('ontouchstart' in window){ //手机浏览器聚焦无法弹出键盘处理
inputPrompt.attr("disabled", "disabled");
_event = "touchstart";
} var init = function(){
if($.trim(target.val())==""){
target.val("");
target.after(inputPrompt);
target.hide();
inputPrompt.show();
inputPrompt.bind(_event,function(){
setTimeout(function(){
target.focus();
});
});
}
};
target.bind({
blur: function(){
init();
},
focus:function(){
inputPrompt.detach();
$(this).show();
},
_reset:function(){
if($.trim(target.val())==""){
init();
}
else{
target.show();
if(inputPrompt){
inputPrompt.hide();
inputPrompt.remove();
}
}
}
});
init();
}; /**
* jQuery 自定义插件
*/
(function ($) {
'use strict';
/**
* 设置输入灰色提示值
* @author rubekid
*/
$.fn.setInputTip = function(options){
return this.each(function(){
var settings = $.extend({}, options||{});
setInputTipValue($(this), settings);
});
}; })(window.jQuery || window.Zepto);
}
页面调用demo:
<input type="text" data-tip="请输入要名称进行查询" id="keyword" name="keyword" />
<script type="text/javascript">
$(function(){
$("#keyword").setInputTip();
});
</script>
效果:
jQuery自定义组件——输入框设置默认提示文字的更多相关文章
- <input>/<textarea>输入框设置默认提示文字(隐藏式)
html代码如下: <tr> <td>签 名:</td> <td><input type="text" nam ...
- 输入框去除默认的文字,jquery方法
需求:所有的输入框获取焦点时,去掉默认的提示文字,失去焦点时如果输入框为空,恢复默认的提示文字. 解决方案:jquery方法,以下有三种,按照利弊,我建议最后一种. 先看html代码: <inp ...
- ASP.NET TextBox 当鼠标点击后清空默认提示文字
ASP.NET TextBox 当鼠标点击后清空默认提示文字 [ 方法一] 前台代码: <div> <asp:TextBox ID="txtName" ru ...
- 基于jQuery的input输入框下拉提示层(自动邮箱后缀名)
基于jQuery的input输入框下拉提示层,方便用户输入邮箱时的提示信息,需要的朋友可以参考下 效果图 // JavaScript Document (function($){ $.fn ...
- jquery自定义组件开发
jquery的组件已经有很多,但是有可能找不到符合我们需求的组件,所以我们可以动手自己封装一个jquery组件. 第一步要知道封装jquery组件的基本语法 (function ($) { $.fn. ...
- Vue 父组件传递给子组件,设置默认值为数组或者对象时
在vue 父子组件传参过程中,传递对象或者数组时,设置默认值为{}或者[] 错误写法: props: { pos: { type: [Object, Array], default: []//这是错误 ...
- ASP.NET TextBox 当鼠标点击后清空默认提示文字[转]
前台文本框里添加下面2个事件属性: OnFocus="javascript:if(this.value=='提示文字') {this.value=''}" OnBlur=" ...
- 给input标签添加默认提示文字
<input name="username" placeholder="请输入用户名" /> placeholder = "提示文字&qu ...
- jsp页面中如何让一个输入框内的提示文字是灰色而输入的文字是黑色
<input name="name" type="text" value="提示的文字" size="30" o ...
随机推荐
- 关于Python文档读取UTF-8编码文件问题
近来接到一个小项目,读取目标文件中每一行url,并逐个请求url,拿到想要的数据. #-*- coding:utf-8 -*- class IpUrlManager(object): def __in ...
- 学习python——博客记录第一天
HELLO WORLD! 今天第一次开立博客,专门记载学习编程语言中的点点滴滴.今日学习内容: 搭建vs code+ python3.5 "ide" 学习廖雪峰python教程 ...
- 看源码之Adapter和AdapterView之间的关系
总述 Android中"列表"的实现其实一个典型的MVC模式,其实中AdapterView相当于是View,负责视图的绘制以及视图的事件响应,Adapter相当于是Controll ...
- net Core 使用MyCat分布式数据库,实现读写分离
net Core 使用MyCat分布式数据库,实现读写分离 目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 MyCat2.0版本很快就发布了,关于MyCat的动态和一些问题, ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析)README 算法的部分英文解释
This is the Readme file for NSGA-II code. About the Algorithm--------------------------------------- ...
- Erget 显示对象
核心显示类: 类 描述 DisplayObject 显示对象基类,所有显示对象均继承自此类 Bitmap 位图,用来显示图片 Shape 用来显示矢量图,可以使用其中的方法绘制矢量图形 TextFie ...
- Xcode7调试-b
Xcode7中苹果为我们增加了两个重要的debug相关功能.了解之后觉得非常实用,介绍给大家. 1.Address Sanitizer: 妈妈再也不用担心 EXC_BAD_ACCESS? EXC_BA ...
- Spring ApplicationContext的事件机制
ApplicationContext的事件机制是观察者设计模式的实现,通过 ApplicationEvent 类和 ApplicationListener 接口,可以实现 ApplicationCon ...
- codeproject
search: http http://www.codeproject.com/Articles/96620/Writing-Http-Handlers-and-Modules-in-IIS-7-5 ...
- Hive SQL运行状态监控(HiveSQLMonitor)
引言 目前数据平台使用Hadoop构建,为了方便数据分析师的工作,使用Hive对Hadoop MapReduce任务进行封装,我们面对的不再是一个个的MR任务,而是一条条的SQL语句.数据平台内部 ...