inputclean外掛程式的用法,inputclean外掛程式

來源:互聯網
上載者:User

inputclean外掛程式的用法,inputclean外掛程式

inputclean外掛程式的用法
語言:javascript
是jquery外掛程式,
目的:給文字框增加×,點擊×,即可清空文字框內容.
:

具體互動效果:
1,當文字框聚焦時,×永久顯示,不管滑鼠游標在不在文字框上面(hover);
2,當文字框失去焦點時,只有滑鼠hover,×才會顯示,滑鼠移開,×消失;
3,點擊×,文字框內容被清空,且文字框自動聚焦.

整個外掛程式不到75行,所以就直接貼代碼了:
檔案名稱:jquery.inputclean.js

/** * Created by huangweii on 2015/7/27. * 用於增加文字框的×,點擊×會清空文字框的內容 */$.fn.inputclean = function (option, callback) {    var settings = $.extend({        inputClearClass: undefined,        deviationTop: undefined/*垂直方向上的誤差*/,        deviationLeft: undefined/*水平方向上的誤差*/,        clickCallback: undefined/*清空文字框之後的回調事件*/,        noFocusAfterClean: undefined/* 清空文字框內容後是否聚焦 */,        parentHoverClass: 'inputParentLi'/*文字框的父元素的class,用於hover時顯示×*/    }, option);    function bootstrap($that) {        var defInputClearClass = 'inputClearBtn';//正常情況下,字型的顏色        if (settings.inputClearClass) {            defInputClearClass = settings.inputClearClass;        }        var cleanBtnTop = 11;        if (settings.deviationTop) {            cleanBtnTop = cleanBtnTop + Number(settings.deviationTop);        }        var cleanBtn = '<i  class="' + defInputClearClass + '"></i>';        var $cleanBtn = $(cleanBtn);        $that.before($cleanBtn);        $that.data('inputclean', true);//用於判斷 是否有×  console.log($that.data('inputclean'))        /* 為什麼要判斷是否有×呢?         如果有×,則有內容時顯示,無內容時不顯示:         if($orgFullName.data('inputclean')){         if($orgFullName.val()){         $orgFullName.prev().show();         }else{         $orgFullName.prev().hide();         }         }         * */        if (settings.parentHoverClass) {//增加父元素的樣式,用於父元素hover時,顯示×            $parentLi = $that.parent();            if (!$parentLi.hasClass(settings.parentHoverClass)) {                $parentLi.addClass(settings.parentHoverClass);            }        }        var width_input = $that.width();        if (settings.deviationLeft) {            width_input = width_input + settings.deviationLeft;        }        console.log('width_input:' + width_input);        $cleanBtn.css('left', (width_input + 12) + 'px');        $cleanBtn.css('top', (cleanBtnTop) + 'px');        $that.css('padding-right', '20px');        $cleanBtn.click(function (e) {            $that.val('');            //$cleanBtn.hide();//隱藏×            if (settings.noFocusAfterClean === undefined || settings.noFocusAfterClean === false) {                console.log(settings.noFocusAfterClean);                $that.focus();            }            if (settings.clickCallback && typeof settings.clickCallback === 'function') {                settings.clickCallback(e);            }        })    }    return this.each(function () {        var $elem = $(this);        bootstrap($elem);        if ($.isFunction(callback)) callback($elem)    })};

參數說明

參數名 含義 說明
inputClearClass ×的樣式 例如 ‘inputClearBtn’
deviationTop 垂直方向上的誤差 預設值為undefined,可能需要設定,特別是在父標籤高度不統一時
deviationLeft 水平方向上的誤差 預設值為undefined,一般不用設定
clickCallback 清空文字框之後的回調事件 比如清空文字框之後,提交按鈕應該置灰
noFocusAfterClean 清空文字框內容後是否聚焦 預設值為undefined,表示聚焦
parentHoverClass 文字框的父元素的class,用於hover時顯示× .inputParentLi:hover .inputClearBtn{display:block;}

調用:

var $orgFullName = $('input[name=orgFullName]');        $orgFullName.inputclean({            deviationTop: 3        }); var $partnerCode = $('input[name=partnerCode]');        $partnerCode.inputclean({            inputClearClass: 'inputClearBtn',            deviationTop: 0,            parentHoverClass: 'inputParentLi',            clickCallback: function (e) {                checkPartnerTrigger();            }        }); var $invoice_title = $('input[name=invoice_title]');    $invoice_title.inputclean({        inputClearClass: 'inputClearBtn',        deviationTop: 0,        parentHoverClass: 'inputParentLi',        deviationLeft: 2    });

我使用的inputClearBtn 樣式為:

.inputClearBtn {    position: absolute;    display: none;    width: 24px;    height: 25px;    background: url('http://www.static.chanjet.com/chanjet/images/appstore/delete.png?v=1437103227783') no-repeat;    cursor: pointer;}

顯示效果:

參考:placeholder外掛程式的使用

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.