亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用JS組件實(shí)現(xiàn)帶ToolTip驗(yàn)證框的實(shí)例代碼

 更新時(shí)間:2017年08月23日 09:15:42   作者:魔方玉米  
這篇文章主要介紹了使用JS組件實(shí)現(xiàn)帶ToolTip驗(yàn)證框的實(shí)例代碼,需要的朋友可以參考下

本組件依賴JQuery

本人測(cè)試的JQuery 是1.8,

兼容IE8,IE9,谷歌,火狐等。

//驗(yàn)證輸入框
function ValidateCompent(input){
  var _input = $(input).clone(true);
  _input.css("height",$(input).css("height"));
  _input.css("width", $(input).css("width"));
  var border =_input.css("border"); 
  this.successIconClass = "icon-tick";//驗(yàn)證通過時(shí)的樣式
  this.validate = false;
  this.faileIconClass = "icon-times";//驗(yàn)證失敗時(shí)的樣式
  var $validateRoot = $('<div class="validate-v1-container"><div class="validate-v1-tooltip"></div></div>');
  var $tooltip = $validateRoot.children(".validate-v1-tooltip");
  var $input = _input;
  if($input != undefined){
    var maxWidth = $input.css("width");
    var maxHeight = $input.css("height");
    $validateRoot.css("display","inline");
    $validateRoot.css("position","relative");
    $validateRoot.css("width",maxWidth);
    $validateRoot.css("height",maxHeight);
    $tooltip.css("width",maxWidth);
    $tooltip.css("padding","0px 5px");
    $tooltip.css("position","absolute");
    $tooltip.css("top","0px");
    $tooltip.css("z-index","999999");
    $tooltip.css("background-color","white");
    $tooltip.css("border","solid 1px rgb(188,188,188)");
    $tooltip.css("left",parseInt(maxWidth) + 10+"px");
    $tooltip.hide();
    var validateOption = $input.attr("data-vali-option");
    if(validateOption != undefined){
      validateOption = JSON.parse(validateOption);
      var that = this;
      var regvali = new Array();
      $tooltip.hide();
      if(validateOption.length == 0){
        return;
      }
      for(var i = 0; i <validateOption.length;i++){
        var message = validateOption[i].message;
        var pattern = validateOption[i].pattern;
        var reg = new RegExp(pattern);
        var messageView = new ValidateMessage(message,that.faileIconClass);
        regvali.push({"reg":reg,"view":messageView});
        $tooltip.append(messageView.dom);
      }
      $tooltip.css("height",(parseInt(maxHeight) +15) * validateOption.length );
      $input.on("textchange focus",function(e){
        $tooltip.show();
        for(var i = 0 ; i < regvali.length; i++){
          if(regvali[i].reg.test($input.val())){
            regvali[i].view.setIconClass(that.successIconClass);
            regvali[i].view.dom.css("color","green");
          }else{
            regvali[i].view.setIconClass(that.faileIconClass);
            regvali[i].view.dom.css("color","red");
          }
        }
      })
      $input.on("blur", function(e) {
        $tooltip.hide();
        for(var i = 0 ; i < regvali.length; i++){
          if(regvali[i].reg.test($input.val())){
            regvali[i].view.setIconClass(that.successIconClass);
            $input.css("border",border);
            that.validate = true;
          }else{
            regvali[i].view.setIconClass(that.faileIconClass);
            $input.css("border","1px solid red");
            that.validate = false;
            break;
          }
        }
      });
      $validateRoot.append($input);
    }else{
      return;
    }
  }
  this.dom = function(){
    return $validateRoot;
  }
  function ValidateMessage(message,iconFontClass){
    var dom = $('<div class="validate-message"><span class="vticon"></span><span class="vmessage"></span></div>');
    var $icon = dom.children(".vticon");
    var $message = dom.children(".vmessage");
    $message.css("line-height","28px");
    $message.css("padding","5px 5px");
    $message.css("padding-right","10px");
    $message.css("word-break","break-all");
    $message.css("word-wrap","break-word");
    $message.css("font-size","14px");
    $message.css("position","relative");
    $message.css("z-index","999999");
    this.setIconClass = function(iconClass){
      $icon.removeClass();
      $icon.addClass("vticon");
      $icon.addClass(iconClass);
    }
    this.getIcon = function(){
      return $icon;
    }
    this.setMessageText = function(_message){
      $message.html(_message);
    }
    this.getMessageText = function(){
      return $message;
    }
    this.setIconClass(iconFontClass);
    this.setMessageText(message);
    this.dom = dom;
  }
  $validateRoot.insertAfter($(input));
  $(input).remove();
}

以下是HTML代碼

<input id="test" data-vali-option='[{"pattern":"[1-9]+","message":"只能輸入1-9的數(shù)"},{"pattern":"[a-z]+","message":"只能輸入a-z的數(shù)"}]' />

使用方法如下

$(function(){
  var c = new ValidateCompent("#test");
});

依賴JQuery,

另外附上JQuery textchange事件的代碼,textchange代碼放在JQuery之后,在使用方法之前。

/**
 * jQuery "splendid textchange" plugin
 * http://benalpert.com/2013/06/18/a-near-perfect-oninput-shim-for-ie-8-and-9.html
 *
 * (c) 2013 Ben Alpert, released under the MIT license
 */
(function($) {
var testNode = document.createElement("input");
var isInputSupported = "oninput" in testNode && 
  (!("documentMode" in document) || document.documentMode > 9);
var hasInputCapabilities = function(elem) {
  // The HTML5 spec lists many more types than `text` and `password` on
  // which the input event is triggered but none of them exist in IE 8 or
  // 9, so we don't check them here.
  // TODO: <textarea> should be supported too but IE seems to reset the
  // selection when changing textarea contents during a selectionchange
  // event so it's not listed here for now.
  return elem.nodeName === "INPUT" &&
    (elem.type === "text" || elem.type === "password");
};
var activeElement = null;
var activeElementValue = null;
var activeElementValueProp = null;
/**
 * (For old IE.) Replacement getter/setter for the `value` property that
 * gets set on the active element.
 */
var newValueProp = {
  get: function() {
    return activeElementValueProp.get.call(this);
  },
  set: function(val) {
    activeElementValue = val;
    activeElementValueProp.set.call(this, val);
  }
};
/**
 * (For old IE.) Starts tracking propertychange events on the passed-in element
 * and override the value property so that we can distinguish user events from
 * value changes in JS.
 */
var startWatching = function(target) {
  activeElement = target;
  activeElementValue = target.value;
  activeElementValueProp = Object.getOwnPropertyDescriptor(
      target.constructor.prototype, "value");
  Object.defineProperty(activeElement, "value", newValueProp);
  activeElement.attachEvent("onpropertychange", handlePropertyChange);
};
/**
 * (For old IE.) Removes the event listeners from the currently-tracked
 * element, if any exists.
 */
var stopWatching = function() {
 if (!activeElement) return;
 // delete restores the original property definition
 delete activeElement.value;
 activeElement.detachEvent("onpropertychange", handlePropertyChange);
 activeElement = null;
 activeElementValue = null;
 activeElementValueProp = null;
};
/**
 * (For old IE.) Handles a propertychange event, sending a textChange event if
 * the value of the active element has changed.
 */
var handlePropertyChange = function(nativeEvent) {
  if (nativeEvent.propertyName !== "value") return;
  var value = nativeEvent.srcElement.value;
  if (value === activeElementValue) return;
  activeElementValue = value;
  $(activeElement).trigger("textchange");
};
if (isInputSupported) {
  $(document)
    .on("input", function(e) {
      // In modern browsers (i.e., not IE 8 or 9), the input event is
      // exactly what we want so fall through here and trigger the
      // event...
      if (e.target.nodeName !== "TEXTAREA") {
        // ...unless it's a textarea, in which case we don't fire an
        // event (so that we have consistency with our old-IE shim).
        $(e.target).trigger("textchange");
      }
    });
} else {
  $(document)
    .on("focusin", function(e) {
      // In IE 8, we can capture almost all .value changes by adding a
      // propertychange handler and looking for events with propertyName
      // equal to 'value'.
      // In IE 9, propertychange fires for most input events but is buggy
      // and doesn't fire when text is deleted, but conveniently,
      // selectionchange appears to fire in all of the remaining cases so
      // we catch those and forward the event if the value has changed.
      // In either case, we don't want to call the event handler if the
      // value is changed from JS so we redefine a setter for `.value`
      // that updates our activeElementValue variable, allowing us to
      // ignore those changes.
      if (hasInputCapabilities(e.target)) {
        // stopWatching() should be a noop here but we call it just in
        // case we missed a blur event somehow.
        stopWatching();
        startWatching(e.target);
      }
    })
    .on("focusout", function() {
      stopWatching();
    })
    .on("selectionchange keyup keydown", function() {
      // On the selectionchange event, e.target is just document which
      // isn't helpful for us so just check activeElement instead.
      //
      // 90% of the time, keydown and keyup aren't necessary. IE 8 fails
      // to fire propertychange on the first input event after setting
      // `value` from a script and fires only keydown, keypress, keyup.
      // Catching keyup usually gets it and catching keydown lets us fire
      // an event for the first keystroke if user does a key repeat
      // (it'll be a little delayed: right before the second keystroke).
      // Other input methods (e.g., paste) seem to fire selectionchange
      // normally.
      if (activeElement && activeElement.value !== activeElementValue) {
        activeElementValue = activeElement.value;
        $(activeElement).trigger("textchange");
      }
    });
}
})(jQuery);

獲取驗(yàn)證結(jié)果

$(function(){
  var c = new ValidateCompent("#test");
  $("#test").click(function(){
    console.log(c.validate);
  });
});

自定義顯示方案

$(function(){
  var c = new ValidateCompent("#test");
  $("#test").click(function(){
    console.log(c.validate);
  });
  c.dom().addClass("你的樣式類");
});

設(shè)置圖標(biāo)字體樣式

$(function(){
  var c = new ValidateCompent("#test");
  $("#test").click(function(){
    console.log(c.validate);
  });
  c.successIconClass = "";//成功時(shí)的樣式
  c.faileIconClass = "";//失敗時(shí)的樣式
});

效果圖如下

分別是成功,部分成功,全部失敗選中,未選中的樣式效果,(勾叉是用的字體css,建議自行尋找字體替代)

這里寫圖片描述這里寫圖片描述這里寫圖片描述這里寫圖片描述

總結(jié)

以上所述是小編給大家介紹的使用JS組件實(shí)現(xiàn)帶ToolTip驗(yàn)證框的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • JavaScript數(shù)組之展開運(yùn)算符詳解

    JavaScript數(shù)組之展開運(yùn)算符詳解

    這篇文章主要給大家介紹了關(guān)于JavaScript數(shù)組之展開運(yùn)算符的相關(guān)資料,你可以通過展開操作符(Spread operator)擴(kuò)展一個(gè)數(shù)組對(duì)象和字符串,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • 原生js實(shí)現(xiàn)拼圖效果

    原生js實(shí)現(xiàn)拼圖效果

    這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)拼圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • vue/js實(shí)現(xiàn)頁面自動(dòng)往上滑動(dòng)效果

    vue/js實(shí)現(xiàn)頁面自動(dòng)往上滑動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了vue/js實(shí)現(xiàn)頁面自動(dòng)往上滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • JavaScript控制音頻和視頻的播放、暫停、音量

    JavaScript控制音頻和視頻的播放、暫停、音量

    HTML<video>元素用于在HTML或者XHTML文檔中嵌入媒體播放器,用于支持文檔內(nèi)的視頻播放,你也可以將<video>標(biāo)簽用于音頻內(nèi)容,在前端中實(shí)現(xiàn)音頻和視頻播放通常涉及使用HTML5的<audio>和<video>元素以及JavaScript來控制這些媒體元素的播放、暫停、音量等屬性
    2023-10-10
  • 用方法封裝javascript的new操作符(一)

    用方法封裝javascript的new操作符(一)

    雖然js是基于對(duì)象的,但在很多時(shí)候會(huì)使用到new這個(gè)操作符。
    2010-12-12
  • javascript實(shí)現(xiàn)倒計(jì)時(shí)關(guān)閉廣告

    javascript實(shí)現(xiàn)倒計(jì)時(shí)關(guān)閉廣告

    這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)倒計(jì)時(shí)關(guān)閉廣告,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • js驗(yàn)證是否為數(shù)字的總結(jié)

    js驗(yàn)證是否為數(shù)字的總結(jié)

    js驗(yàn)證是否為數(shù)字的總結(jié),需要的朋友可以參考一下
    2013-04-04
  • Bootstrap table使用方法匯總

    Bootstrap table使用方法匯總

    這篇文章主要為大家總結(jié)了Bootstrap table簡(jiǎn)單使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • JS中的算法與數(shù)據(jù)結(jié)構(gòu)之棧(Stack)實(shí)例詳解

    JS中的算法與數(shù)據(jù)結(jié)構(gòu)之棧(Stack)實(shí)例詳解

    這篇文章主要介紹了JS中的算法與數(shù)據(jù)結(jié)構(gòu)之棧(Stack),結(jié)合實(shí)例形式詳細(xì)分析了js中棧的概念、原理、定義及常見使用方法,需要的朋友可以參考下
    2019-08-08
  • layui 上傳圖片 返回圖片地址的方法

    layui 上傳圖片 返回圖片地址的方法

    今天小編就為大家分享一篇layui 上傳圖片 返回圖片地址的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-09-09

最新評(píng)論