欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

jQuery 仿百度输入标签插件附效果图_jquery

程序员文章站 2022-04-02 16:00:28
...
1、先上效果图
jQuery 仿百度输入标签插件附效果图_jquery
2、调用方式

html页面调用

3、tagsinput.css样式

.clearfix:after
{
clear: both;
content: " ";
display: block;
height: 0;
}
.tags-wrapper
{
width: 500px;
position:relative;
}
#addTagWrap
{
background: none repeat scroll 0 0 #FFFFFF;
border: 1px solid #D9D9D9;
padding: 0 5px;
}
#addTagWrap .inner-tag-wrapper,.layer-tag-name
{
background: none repeat scroll 0 0 #40A8CD;
border-radius: 3px;
color: #FFFFFF;
float: left;
height: 26px;
line-height: 26px;
margin: 4px 6px 0 0;
padding: 0 5px 0 10px;
white-space: nowrap;
}
#addTagWrap .inner-tag-close
{
color: #A0D4E6;
font-family: "宋体" ,sans-serif;
margin-left: 4px;
text-decoration: none;
}
#tagInput
{
background: none repeat scroll 0 0 #FFFFFF;
border: medium none;
margin: 0;
height: 24px;
line-height: 24px;
overflow: hidden;
padding: 5px;
width: 215px;
}
#tagInput:focus{ outline:none }
.layer-tags-wrapper
{
border: 1px solid #DADADA;
border-top:0;
overflow: auto;
position:absolute;
left:0;
right:0;
display:none;
background: none repeat scroll 0 0 #FFFFFF;
}
.layer-tags-wrapper .layer-tags-box
{
padding: 0 5px;
}
.layer-tags-wrapper .layer-tags-left
{
float: left;
text-align: center;
padding-right: 5px;
margin-top: 4px;
height: 26px;
line-height: 26px;
}
.layer-tags-wrapper .layer-tags-right
{
overflow: auto;
}
.layer-tags-wrapper .layer-tag-name
{
padding-right: 10px;
text-decoration: none;
}
.layer-tags-foot
{
height: 30px;
line-height: 30px;
color: #999999;
padding-left:5px;
}
.layer-tags-foot-top
{
margin-top:5px;
border-top:1px dotted #C9C9C9;
}
.message-box
{
background: none repeat scroll 0 0 rgba(0, 0, 0, 0.35);
color: #FFFFFF;
width: 300px;
min-height: 50px;
line-height: 50px;
top: 50%;
left: 50%;
margin-top: -50px; /*注意这里必须是DIV高度的一半*/
margin-left: -150px; /*这里是DIV宽度的一半*/
position: fixed !important; /*FF IE7*/
position: absolute; /*IE6*/
z-index: 999;
text-align: center;
border-radius: 5px;
}

4、jquery.tagsinput.js

/*仿百度标签输入v0.1
* @name jquery.tagsinput.js
* @version 0.1
* @author liping
* @date 2014/06/10
* @Email:272323108@qq.com
*/
(function ($) {
$.fn.TagsInput = function (options) {
//默认参数
var defaults = {
usedTags: "",
hotTags: "",
tagNum: 0,
maxWords: 0
};
//用传入参数覆盖了默认值
var opts = $.extend(defaults, options);
//对象
var $this = $(this);
$this.hide();
var arrayTags;
var strHtml;
strHtml = "
"; strHtml += "
"; strHtml += ""; strHtml += "
"; if (opts.usedTags != "") { strHtml += "
记忆标签
"; arrayTags = opts.usedTags.split('|'); for (i = 0; i " + arrayTags[i] + ""; } strHtml += "
"; } if (opts.hotTags != "") { strHtml += "
热门标签
"; arrayTags = opts.hotTags.split('|'); for (i = 0; i " + arrayTags[i] + ""; } strHtml += "
"; } if (opts.tagNum != 0 && opts.maxWords != 0) { strHtml += "
最多可添加" + opts.tagNum + "个标签,每个标签不超过" + opts.maxWords + "个汉字
"; } else if (opts.tagNum != 0 && opts.maxWords == 0) { strHtml += "
最多可添加" + opts.tagNum + "个标签
"; } else if (opts.tagNum == 0 && opts.maxWords != 0) { strHtml += "
每个标签不超过" + opts.maxWords + "个汉字
"; } else { strHtml += "
标签个数最好少于10个,每个标签最好不超过10个汉字
"; } strHtml += "
"; $(strHtml).insertAfter($this); if ($(".layer-tag-name").length > 0) { $(".layer-tags-foot").addClass("layer-tags-foot-top"); } var inputTags = $this.val(); arrayTags = inputTags.split('|'); for (i = 0; i 0 && num > opts.maxWords) { MessageBox("单个标签最多" + opts.maxWords + "个汉字"); return false; } num = 0; } var tags = $("#addTagWrap .inner-tag-name"); var flag = true; var s = ""; tags.each(function () { if ($(this).text() == obj) { flag = false; return false; } num++; s += $(this).text() + "|"; }); if (opts.tagNum > 0 && num >= opts.tagNum) { MessageBox("最多可添加" + opts.tagNum + "个标签"); return false; } if (flag) { $(".added-tags-wrapper").append("
" + obj + "×
"); $(".added-tags-wrapper .inner-tag-close:last").click(function () { $(this).parent().remove(); }); s += obj + "|"; if (s.length > 0) { s = s.substring(0, s.length - 1); $this.val(s); } return true; } else { MessageBox("该标签已经存在"); return false; } } function MessageBox(obj) { $("
" + obj + "
").appendTo("body"); $(".message-box").delay(1000).fadeOut("slow", function () { $(this).remove(); }); } }; })(jQuery);