關(guān)于layui標(biāo)簽輸入框inputTags的相關(guān)介紹

layui框架是一款采用自身模塊規(guī)范編寫的前端UI框架,門檻極低,拿來即用。這篇文章主要為大家詳細(xì)介紹了layui標(biāo)簽輸入框inputTags,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

阜南ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

關(guān)于layui標(biāo)簽輸入框inputTags的相關(guān)介紹

layui標(biāo)簽輸入框inputTags樣式:

關(guān)于layui標(biāo)簽輸入框inputTags的相關(guān)介紹

目錄結(jié)構(gòu):

關(guān)于layui標(biāo)簽輸入框inputTags的相關(guān)介紹

頁面代碼:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>inputTags</title>
		<link rel="stylesheet" href="plugins/layui/css/layui.css" />

	</head>

	<body>
		<div class="tags" id="tags">
			<input type="text" name="" id="inputTags" placeholder="回車生成標(biāo)簽" autocomplete="off">
		</div>
	</body>

</html>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script>
	layui.config({
		base: 'js/',
	}).use(['inputTags'], function() {
		var inputTags = layui.inputTags;
		inputTags.render({
			elem: '#inputTags', //定義輸入框input對(duì)象
			content: [], //默認(rèn)標(biāo)簽
			aldaBtn: true, //是否開啟獲取所有數(shù)據(jù)的按鈕
			done: function(value) { //回車后的回調(diào)
				console.log("剛剛輸入標(biāo)簽===="+value)
			}
		})
	})
</script>

**inputTags.js**

/*
* @Author: layui-2
* @Date:   2018-08-31 11:40:42
* @Last Modified by:   layui-2
* @Last Modified time: 2018-09-04 14:44:38
*/
layui.define(['jquery','layer'],function(exports){
  "use strict";
  var $ = layui.jquery,layer = layui.layer
  

  //外部接口
  ,inputTags = {
    config: {}

    //設(shè)置全局項(xiàng)
    ,set: function(options){
      var that = this;
      that.config = $.extend({}, that.config, options);
      return that;
    }

    // 事件監(jiān)聽
    ,on: function(events, callback){
      return layui.onevent.call(this, MOD_NAME, events, callback)
    }
    
  }

   //操作當(dāng)前實(shí)例
  ,thisinputTags = function(){
    var that = this
    ,options = that.config;

    return {
      config: options
    }
  }

  //字符常量
  ,MOD_NAME = 'inputTags'


  // 構(gòu)造器
  ,Class = function(options){
    var that = this;
    that.config = $.extend({}, that.config, inputTags.config, options);
    that.render();
  };

   //默認(rèn)配置
  Class.prototype.config = {
    close: false  //默認(rèn):不開啟關(guān)閉按鈕
    ,theme: ''   //背景:顏色
    ,content: [] //默認(rèn)標(biāo)簽
    ,aldaBtn: false //默認(rèn)配置
  };

  // 初始化
  Class.prototype.init = function(){
    var that = this
    ,spans = ''
    ,options = that.config
    ,span = document.createElement("span"),
    spantext = $(span).text("獲取全部數(shù)據(jù)").addClass('albtn');
    if(options.aldaBtn){
      $('body').append(spantext)
    }
    
    $.each(options.content,function(index,item){
      spans +='<span><em>'+item+'</em><button type="button" class="close">×</button></span>';
      // $('<div class="layui-flow-more"><a href="javascript:;">'+ ELEM_TEXT +'</a></div>');
    })
    options.elem.before(spans)
    that.events()
  }

  Class.prototype.render = function(){
    var that = this
    ,options = that.config
    options.elem = $(options.elem);
    that.enter()
  };

  // 回車生成標(biāo)簽
  Class.prototype.enter = function(){
    var that = this
    ,spans = ''
    ,options = that.config;
    options.elem.focus();
    options.elem.keypress(function(event){  
      var keynum = (event.keyCode ? event.keyCode : event.which);  
      if(keynum == '13'){  
        var $val = options.elem.val().trim();
        if(!$val) return false;
        if(options.content.indexOf($val) == -1){
          options.content.push($val)
          that.render()
          spans ='<span><em>'+$val+'</em><button type="button" class="close">×</button></span>';
          options.elem.before(spans)
        }
        options.done && typeof options.done === 'function' && options.done($val);
        options.elem.val('');
      }   
    })
  };
  
  //事件處理
  Class.prototype.events = function(){
     var that = this
    ,options = that.config;
    $('.albtn').on('click',function(){
      console.log(options.content)
    })
    $('#tags').on('click','.close',function(){
      var Thisremov = $(this).parent('span').remove(),
      ThisText = $(Thisremov).find('em').text();
      options.content.splice($.inArray(ThisText,options.content),1)
    })
  };

  //核心入口
  inputTags.render = function(options){
    var inst = new Class(options);
    inst.init();
    return thisinputTags.call(inst);
  };
  exports('inputTags',inputTags);
}).link('css/inputTags.css')

關(guān)于關(guān)于layui標(biāo)簽輸入框inputTags的相關(guān)介紹就分享到這里了,當(dāng)然并不止以上和大家分析的辦法,不過小編可以保證其準(zhǔn)確性是絕對(duì)沒問題的。希望以上內(nèi)容可以對(duì)大家有一定的參考價(jià)值,可以學(xué)以致用。如果喜歡本篇文章,不妨把它分享出去讓更多的人看到。

當(dāng)前名稱:關(guān)于layui標(biāo)簽輸入框inputTags的相關(guān)介紹
文章地址:http://muchs.cn/article0/ihdeoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、動(dòng)態(tài)網(wǎng)站、用戶體驗(yàn)、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)