最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Contenteditable: How to completely remove span when pressing del or backspace - Stack Overflow

programmeradmin4浏览0评论

I have a contenteditable div, as shown in HTML below (caret marked by |).

I would like to remove span.label when pressing backspace or delete (i.e. the span acts as a single letter, thus to the user it looks as if Name was deleted in one single keypress)

<div contenteditable="true">
   Hallo, <span class="label">Name</span>|,
   this is a demonstration of placeholders!
   Sincerly, your
   <span class="label">Author</span>
</div>

I have a contenteditable div, as shown in HTML below (caret marked by |).

I would like to remove span.label when pressing backspace or delete (i.e. the span acts as a single letter, thus to the user it looks as if Name was deleted in one single keypress)

<div contenteditable="true">
   Hallo, <span class="label">Name</span>|,
   this is a demonstration of placeholders!
   Sincerly, your
   <span class="label">Author</span>
</div>
Share Improve this question edited Aug 11, 2017 at 1:02 toastrackengima 8,7424 gold badges53 silver badges62 bronze badges asked Aug 10, 2017 at 23:54 TobiTobi 1,3121 gold badge20 silver badges46 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 16

You need to check if the cursor is at the exact position of the end of the span, and if so - remove it:

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();
                } else {
                    el.classList.add('highlight');
                }
                return;
            }
        }
    }
    event.target.querySelectorAll('span.label.highlight').forEach(function(el) { el.classList.remove('highlight');})
});
span.label.highlight {
    background: #E1ECF4;
    border: 1px dotted #39739d;
}
<div contenteditable="true">
 Hallo, <span class="label">Name</span>|,
 this is a demonstration of placeholders!
</div>

I didn't implement the del key functionality, but the general idea is there and I think you can plete it based on the above example

You can listened to onKeyDown event on the div:

<div contenteditable="true" id="editable">
 Hallo, <span class="label">Name</span>|,
 this is a demonstration of placeholders!
</div>

Here is the event listening handler:

        document.addEventListener('keydown', function (e) {
            var keycode = event.keyCode;
            console.log(keycode);
            if (keycode === 8 || keycode === 46) {
                var spnLables = document.querySelectorAll('#editable span.label');
                if (spnLables.length) {
                    Array.prototype.forEach.call(spnLables, function (thisnode) {
                        document.getElementById('editable').removeChild(thisnode);
                    });

                }

            }
        });
发布评论

评论列表(0)

  1. 暂无评论