JSFiddle - React, Tailwind, and code Playground
by Radioactive
HTML
<div id="boxTags">
<input type="hidden" id="hiddenTags"/>
<ul id="ulTags" style="clear:both;">
<li id="newTagInput"><input type="text" id="inputTag" /></li>
</ul>
<input id="fullTags" type="text" value="" placeholder="full,full,">
<div style="clear:both;"></div>
</div>
<hr>
test images:<br/>
https://jsfiddle.net/img/logo.png<br/>
https://php.ru/images/logo.gif
CSS
#ulTags {
padding: 0;
margin: 0;
}
#ulTags .li_tags {
border: 1px solid #e3e3e3;
background: #e3e3e3;
display: inline-block;
float: left;
list-style: none;
position: relative;
width: 32%;
padding:1.1666%;
font-size: 0;
}
#ulTags .li_tags *{
font-size: 16px;
}
#ulTags .li_tags img{
width: 100%;
}
#ulTags .li_tags:hover {
border-color: #0099cc;
}
#newTagInput {
display: inline;
list-style: none;
}
.a_tag {
color: #000000;
text-decoration: none;
font-size: 12px;
margin-top: 10px;
display: block;
word-break: break-all;
}
.a_tag:hover {
color: #000000;
}
.del {
font-size: 12px;
text-decoration: none;
font-weight: bold;
padding: 1px 4px 1px 4px;
background: #0099cc;
color: #ffffff;
position: absolute;
right: 5px;
top: 5px;
}
.del:hover {
background: #ff0000;
}
JavaScript
var arrayTags = [""]; // Массив, который содержит метки
var index = 0;
// Удаление элемента из массива
function removeByValue(arr, val) {
for(var i=0; i<arr.length; i++) {
if(arr[i] == val) {
arr.splice(i, 1);
break;
}
}
index--;
}
// Удаление метки из списка
function removeTag(el) {
tag = $(el).prev().html();
$("#tag-"+tag).remove();
removeByValue(arrayTags, tag);
$("#inputTag").focus();
}
$(document).ready(function() {
var inputWidth = 16;
// Вставка метки в список
function insertTag(tag) {
var liEl = '<li id="tag-'+tag+'" class="li_tags"><img src="'+tag+'" alt="'+tag+'">'+
'<span href="javascript://" class="a_tag">'+tag+'</span>'+
'<a href="" onclick="removeTag(this); return false;"'+
' class="del" id="del_'+tag+'">×</a>'+
'</li>';
return liEl;
}
$("#hiddenTags").val("");
// Проверяем нажатие клавиши
$("#inputTag").keydown(function(event) {
var textVal = jQuery.trim($(this).val()).toLowerCase();
var keyCode = event.which;
if ((47 < keyCode && keyCode < 106) || (keyCode == 32)) {
if (keyCode != 32) {
} else if (keyCode == 32 && (textVal != '')) {
// Пользователь создает новую метку
var isExist = jQuery.inArray(textVal, arrayTags);
if (isExist == -1) {
// Вставляем новую метку (видимый элемент)
$(insertTag(textVal)).insertBefore("#newTagInput");
// Вставляем новую метку в массив JavaScript
arrayTags[index] = textVal;
index++;
}
$(this).val("");
} else {
$(this).val("");
}
}
});
});