在Web下輸入密碼時提示大寫鎖定鍵的jQuery插件
功能介紹:
在Web下輸入密碼時提示大寫鎖定鍵,封裝成jq插件方便有需要的同學!
使用:
$("#txtPWD").capsLockTip();
截圖預覽:
代碼(2012-01-21 16:21最后修改):
1. 使用公用靜態對象,使多個密碼框共享狀態。
- 增加focus 和 blur事件,提示更方便及時。
- 優化代碼。</strong></span></p>
//=============== 大寫鎖定鍵打開提示 ============================================// / 使用:$("#txtPWD").capsLockTip();/ (function ($) { $.fn.extend({
});capsLockTip: function (divTipID) { return this.each(function () { //創建對象實例并保存。 //獲取實例對象:var api = $("#txtPWD").data("txtPWD"); var ins = new $.CapsLockTip($(this)); $(this).data(this.id, ins); }); }
//創建一個實例。
//___target jq目標對象。
//___divTipID 顯示提示文本的div。
$.CapsLockTip = function (___target) {
//設置當前實例的配置參數。
this.target = ___target;
var _this = this;
$(document).ready(function () {
//創建顯示大寫鎖定的div。
if(null == $.fn.capsLockTip.divTip){
$("body").append("<div id='divTip__985124855558842555' style='width:100px; height:15px; padding-top:3px; display:none; position:absolute; z-index:9999999999999; text-align:center; background-color:#FDF6AA; color:Red; font-size:12px; border:solid 1px #DBC492; border-bottom-color:#B49366; border-right-color:#B49366;'>大寫鎖定已打開</div>");
$.fn.capsLockTip.divTip = $("#divTip__985124855558842555");
}
_this.target.bind("keypress", function (_event) {
var e = _event || window.event;
var kc = e.keyCode || e.which;
var isShift = e.shiftKey || (kc == 16) || false;
$.fn.capsLockTip.capsLockActived = false;
if ((kc >= 65 && kc <= 90 && !isShift) || (kc >= 97 && kc <= 122 && isShift))
$.fn.capsLockTip.capsLockActived = true;
_this.showTips($.fn.capsLockTip.capsLockActived);
});
_this.target.bind("keydown", function (_event) {
var e = _event || window.event;
var kc = e.keyCode || e.which;
if (kc == 20 && null != $.fn.capsLockTip.capsLockActived){
$.fn.capsLockTip.capsLockActived = !$.fn.capsLockTip.capsLockActived;
_this.showTips($.fn.capsLockTip.capsLockActived);
}
});
_this.target.bind("focus", function (_event) {
if (null != $.fn.capsLockTip.capsLockActived)
_this.showTips($.fn.capsLockTip.capsLockActived);
});
_this.target.bind("blur", function (_event) {
_this.showTips(false);
});
});
//顯示或隱藏大寫鎖定提示。
this.showTips = function (display) {
if (display) {
var offset = _this.target.offset();
$.fn.capsLockTip.divTip.css("left", offset.left + "px");
$.fn.capsLockTip.divTip.css("top", offset.top + _this.target[0].offsetHeight + 3 + "px");
$.fn.capsLockTip.divTip.show();
}
else {
$.fn.capsLockTip.divTip.hide();
}
};
//jq控件公用靜態對象。
//提示框。
$.fn.capsLockTip.divTip = null;
//大寫鎖定鍵狀態
$.fn.capsLockTip.capsLockActived = null;
};
})(jQuery);</pre>轉自:http://www.cnblogs.com/mrhgw/archive/2012/04/21/2461566.html
本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!