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自定义组件——输入框设置默认提示文字的更多相关文章

  1. <input>/<textarea>输入框设置默认提示文字(隐藏式)

    html代码如下: <tr>    <td>签   名:</td>    <td><input type="text" nam ...

  2. 输入框去除默认的文字,jquery方法

    需求:所有的输入框获取焦点时,去掉默认的提示文字,失去焦点时如果输入框为空,恢复默认的提示文字. 解决方案:jquery方法,以下有三种,按照利弊,我建议最后一种. 先看html代码: <inp ...

  3. ASP.NET TextBox 当鼠标点击后清空默认提示文字

    ASP.NET TextBox 当鼠标点击后清空默认提示文字 [ 方法一] 前台代码: <div>    <asp:TextBox ID="txtName" ru ...

  4. 基于jQuery的input输入框下拉提示层(自动邮箱后缀名)

    基于jQuery的input输入框下拉提示层,方便用户输入邮箱时的提示信息,需要的朋友可以参考下     效果图   // JavaScript Document (function($){ $.fn ...

  5. jquery自定义组件开发

    jquery的组件已经有很多,但是有可能找不到符合我们需求的组件,所以我们可以动手自己封装一个jquery组件. 第一步要知道封装jquery组件的基本语法 (function ($) { $.fn. ...

  6. Vue 父组件传递给子组件,设置默认值为数组或者对象时

    在vue 父子组件传参过程中,传递对象或者数组时,设置默认值为{}或者[] 错误写法: props: { pos: { type: [Object, Array], default: []//这是错误 ...

  7. ASP.NET TextBox 当鼠标点击后清空默认提示文字[转]

    前台文本框里添加下面2个事件属性: OnFocus="javascript:if(this.value=='提示文字') {this.value=''}" OnBlur=" ...

  8. 给input标签添加默认提示文字

    <input name="username" placeholder="请输入用户名" /> placeholder = "提示文字&qu ...

  9. jsp页面中如何让一个输入框内的提示文字是灰色而输入的文字是黑色

    <input name="name" type="text"  value="提示的文字" size="30" o ...

随机推荐

  1. 利用Php ssh2扩展实现svn自动提交到测试服务器

    1.安装ssh2扩展 (1)window . 下载 php extension ssh2 下载地址 http://windows.php.net/downloads/pecl/releases/ssh ...

  2. Js点餐加减数量

    <button class="add-on" onclick="chgNum(1,'del')" ><i class="icon-m ...

  3. 转:如何查看linux版本 如何查看LINUX是多少位

    原文来自于:http://blog.csdn.net/hongweigg/article/details/7192471 一.如何得知自己正在使用的linux是什么版本呢,下面的几种方法将给你带来答案 ...

  4. PartialFunction(偏函数)

    val one:PartialFunction[Int,String]={ case 1 => "one" case 2 => "two" case ...

  5. poj 3020Antenna Placement

    http://poj.org/problem?id=3020 #include<cstdio> #include<cstring> #include<algorithm& ...

  6. LINUX BASH SHELL,小小学习一下

    注意点要注意的: 整数运算一般通过 let 和 expr 这两个指令来实现,如对变量 x 加 1 可以写作:let "x = $x + 1" 或者 x=`expr $x + 1` ...

  7. 如何通过HOOK改变windows的API函数(找到函数的相对偏移)

    我们知道,系统函数都是以DLL封装起来的,应用程序应用到系统函数时,应首先把该DLL加载到当前的进程空间中,调用的系统函数的入口地址,可以通过GetProcAddress函数进行获取.当系统函数进行调 ...

  8. ASP.NET状缓存Cache的应用-提高数据库读取速度

    原文:ASP.NET状缓存Cache的应用-提高数据库读取速度 一. Cache概述       既然缓存中的数据其实是来自数据库的,那么缓存中的数据如何和数据库进行同步呢?一般来说,缓存中应该存放改 ...

  9. COJ 1006 树上操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=979 WZJ的数据结构(六) 难度级别:D: 运行时间限制:1000ms: ...

  10. 前端问题——png图片在IE6下透明失效,解决办法

    今天,一位同事问我问题,png 图片在IE6下透明背景失效. 解决办法,在网上查了很多,最后还是采用两种方案来解决这个问题 1.把这个网页的png格式图片变更为gif格式的图片.问题解决 2.就是让这 ...