我需要在同一个输入框中添加标签和文本.普通文本可以一次删除一个字符.将从一个预定义的特定单词集中选择的标签将一次全部删除.普通文本和标签将位于同一个框中.
链接到小提琴链接 到目前为止我已经尝试过
document.querySelector('.selectable-icons').addEventListener('click', function(e) {
document.querySelector('[contenteditable]').appendChild(e.target.cloneNode(true));
});
document.querySelector('div').addEventListener('keydown', function(event) {
// Check for a backspace
if (event.which == 8) {
s = window.getSelection();
r = s.getRangeAt(0)
el = r.startContainer.parentElement
// Check if the current element is the .label
if (el.classList.contains('label')) {
// Check if we are exactly at the end of the .label element
if (r.startOffset == r.endOffset && r.endOffset == el.textContent.length) {
// prevent the default delete behavior
event.preventDefault();
if (el.classList.contains('highlight')) {
// remove the element
el.remove(); …Run Code Online (Sandbox Code Playgroud)