我在选择正确的插件时也遇到了问题。最后我自己实现了taginput,不难。代码 :
HTML:
<p>Tags : </p><input type="text" name="irrelevant" id="tags" placeholder="add tag">
JavaScript(JS):
var id=1;
function remove(id) {
var element = document.getElementById(id);
element.parentNode.removeChild(element);
}
function addTag(e,name) {
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
var entry=document.getElementById(name);
entry.value=entry.value.toLowerCase();
if (keyCode == '8' && entry.value.length==0) {
if (entry.previousSibling.className=="btn btn-secondary margin") {
entry.previousSibling.parentNode.removeChild(entry.previousSibling)
}
return;
}
if (keyCode != '13' || !/[a-z]$/.test(entry.value) || document.getElementById(name+"_"+entry.value) != null) {
return;
}
var id;
var btn = document.createElement("button");
var t = document.createTextNode(entry.value);
btn.appendChild(t);
btn.setAttribute("id",name+"_"+entry.value)
btn.className="btn btn-secondary margin"
btn.setAttribute("onclick","remove('"+name+"_"+entry.value+"')")
var insertedNode = entry.parentNode.insertBefore(btn, entry);
entry.value=""
}
document.getElementById('tags').onkeypress=function(e) { addTag(e,"tags"); }
基本上,您只是检查是否按下了键并且条目的格式正确,并相应地在条目之前添加标签,这些标签只是按钮。只使用纯 JS。