简体   繁体   English

Contenteditable:如何在按del或退格时完全删除跨度

[英]Contenteditable: How to completely remove span when pressing del or backspace

I have a contenteditable div, as shown in HTML below (caret marked by | ). 我有一个contenteditable div,如下面的HTML所示(插入标记为| )。

I would like to remove span.label when pressing backspace or delete (ie the span acts as a single letter, thus to the user it looks as if Name was deleted in one single keypress) 我想在按退格键删除时删除span.label (即span作为单个字母,因此对于用户来说,看起来好像在一个单键中删除了Name

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

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 complete it based on the above example 我没有实现del键功能,但总体思路就在那里,我认为你可以根据上面的例子来完成它

You can listened to onKeyDown event on the div: 你可以在div上听取onKeyDown事件:

<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);
                    });

                }

            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Html:Contenteditable 如何在按退格键后删除元素之前检测? - Html: Contenteditable how to detect before deleting element after pressing backspace? 删除一个 <li> 按下跨度时 - Remove a <li> when pressing a span 如何使用DEL或BACKSPACE键从组合框中删除焦点条目? - How to remove the focused entry from a Combobox with the DEL or BACKSPACE key? 如何防止使用 contenteditable 删除跨度内? - How to prevent remove inside span using contenteditable? 按Tab,空格,删除和退格键时如何计算字符? - How to count characters when pressing tab, space,delete and backspace? 如何从contenteditable span元素的开头删除enter(换行符)? - How to remove enter (linebreak) from the beginning of contenteditable span element? 如何删除<span>退格</span>插入<span>?</span> - How to delete inserted <span> on backspace? 按退格键时禁用向后导航,允许在字段中退格 - Disable backward navigation when pressing backspace, allow backspace in fields contenteditable div:IE8不满意退格删除HTML元素 - contenteditable div: IE8 not happy with backspace remove of HTML element 当用户尝试更改 span 的文本时,如何在 contentEditable span/div 项目上显示下拉列表 - How to show a dropdown list on a contentEditable span/div item when user tries to change the text of span
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM