我正在开发一个网络应用程序,允许用户通过标签发布内容,但问题是,如果标签被comma并且文本字段值仍然相同,只是用户的视图会有所不同。
一个例子是 YouTube 或 StackOverflow,目前我不需要它来检查数据库或任何东西。
Thanks!
类似于 Stack Overflow 的功能:
- 允许字母数字和
+-.#
(并修剪空格!)
- 转换为小写
- Create automatically the Tag Box on focusOut Enter , (add more delimited by
|
pipe)
- 点击删除标签
jQuery($ => { // DOM ready and $ alias in scope.
// TAGS BOX
$("#tags input").on({
focusout() {
var txt = this.value.replace(/[^a-z0-9\+\-\.\#]/ig,''); // allowed characters
if(txt) $("<span/>", {text:txt.toLowerCase(), insertBefore:this});
this.value = "";
},
keyup(ev) {
if(/(,|Enter)/.test(ev.key)) $(this).focusout();
}
});
$("#tags").on("click", "span", function() {
$(this).remove();
});
});
#tags{
float:left;
border:1px solid #ccc;
padding:5px;
font-family:Arial;
}
#tags > span{
cursor:pointer;
display:block;
float:left;
color:#fff;
background:#789;
padding:5px;
padding-right:25px;
margin:4px;
}
#tags > span:hover{
opacity:0.7;
}
#tags > span:after{
position:absolute;
content:"×";
border:1px solid;
padding:2px 5px;
margin-left:3px;
font-size:11px;
}
#tags > input{
background:#eee;
border:0;
margin:4px;
padding:7px;
width:auto;
}
<div id="tags">
<span>php</span>
<span>c++</span>
<span>jquery</span>
<input type="text" value="" placeholder="Add a tag" />
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)