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 ...
随机推荐
- 初涉JavaScript模式 (12) : 沙箱模式
引子 上一篇说了模块模式,而对于其中的命名空间模式其实也是有着一些问题,比如每添加一个模块或则深入叠加都会导致长命名,并且对于多个库的不同版本同时运行,一不小心就会污染全局标识,而这两天也发现了JSe ...
- 如何使一个你没有源代码的DLL文件变为强命名的DLL
转载: 有时候你会需要一个DLL是强命名的,比如你想把这个文件放到GAC里面.如果这是一个第三方的DLL,你没有源代码,这会是一件比较麻烦的事情.有一个方法可以解决这个问题. 在VS.NET的命名行窗 ...
- Websocket 与代理服务器如何交互? How HTML5 Web Sockets Interact With Proxy Servers
How HTML5 Web Sockets Interact With Proxy Servers Posted by Peter Lubberson Mar 16, 2010 With the re ...
- a便签 rel属性改变链接打开页面的方式
<body> XHTML: <a href="http://www.baidu.com" rel="external">Baidu &l ...
- Javascript中的onclick事件
示例: <script type="text/javascript"> function onSubmit() { alert("保存数据"); } ...
- Struts2 DMI的使用
Struts2的Action类中可以放置多个方法并在struts.xml中配置供页面调用.只要方法符合execute()方法的标准即返回类型是String就可以了. 同一个Action多个方法调用的方 ...
- windows下和linux下 Redis 安装
Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...
- Buy the Ticket HDU 1133 递推+大数
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目大意: 有m+n个人去买电影票,每张电影票50元, m个人是只有50元一张的, n个人 ...
- Moss的使用
- 跑马灯js
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...