简单实现的JQuery文本框水印插件
更新时间:2016年06月14日 09:56:08 作者:Jaxu
这篇文章主要为大家详细介绍了简单实现JQuery文本框水印插件的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
采用JQuery实现文本框的水印效果非常容易,效果如下:
代码片段,定义要应用水印效果的文本框的样式: .watermark { color: #cccccc; }
将JavaScript代码封装成JQuery的插件:
(function ($) { $.fn.watermark = function (options) { var settings = $.extend({ watermarkText: "Input something here", className: "watermark" }, options); return this.each(function () { if ($(this).val().length == 0 || $(this).val() == settings.watermarkText) { //init, set watermark text and class $(this).val(settings.watermarkText).addClass(settings.className); } //if blur and no value inside, set watermark text and class again. $(this).blur(function () { if ($(this).val().length == 0) { $(this).val(settings.watermarkText).addClass(settings.className); } }); //if focus and text is watermrk, set it to empty and remove the watermark class $(this).focus(function () { if ($(this).val() == settings.watermarkText) { $(this).val('').removeClass(settings.className); } }); }); } })(jQuery);
接下来直接在页面上使用:
<div class="search_box"> <input id="tb_search" type="text" /> </div> <script type="text/javascript"> $(document).ready(function () { $("#tb_search").watermark({ watermarkText: "站内检索", className: "watermark", }); }); </script>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
jQuery中的bind绑定事件与文本框改变事件的临时解决方法
暂时没有想到什么好的解决办法,我现在加了个浏览器判断非ie的话就注册blur事件,这样有个问题就是blur实在别的控件活动焦点的时候,txtStation控件注册的方法是为了填充它紧挨着的一个下拉列表2010-08-08基于jQuery的树控件实现代码(asp.net+json)
一个自己写jQuery的树控件,后台用的是asp.net,其实只要服务器返回json就可以了2010-07-07
最新评论