html水印文本框,简单实现的JQuery文本框水印插件

采用JQuery实现文本框的水印效果非常容易,效果如下:

118580c9a3f98e25a6e63562279ad75c.png

代码片段,定义要应用水印效果的文本框的样式:.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);

接下来直接在页面上使用:

$(document).ready(function () {

$("#tb_search").watermark({

watermarkText: "站内检索",

className: "watermark",

});

});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值