简体   繁体   English

如何在Enter键上操作列表元素按

[英]How to manipulate list element on Enter key press

I want to allow the editing of an ordered list using contenteditable. 我想允许使用contenteditable编辑有序列表。 When the user changes or adds in a new element in the list I want to be able to manipulate the text (eg wrap in span tag, replace text etc). 当用户更改或添加列表中的新元素时,我希望能够操作文本(例如,在span标记中换行,替换文本等)。

I've created a listener for the Enter key and can get the last list element value. 我已经为Enter键创建了一个监听器,并且可以获取最后一个列表元素值。 I've tried to change this and replace with the new value. 我试图改变它并用新值替换。 However this populates the new list element created on the enter press. 但是,这会填充在enter press上创建的新列表元素。

<div>
   <ol contenteditable=true class="editor">
    <li><br></li>
   </ol>
</div>
$('.editor' ).on('keydown .editable', function(e){
    if ( e.keyCode === 13 ) {   

    var insertText = "<span>"+e.target.lastElementChild.innerText+"</span>";
    e.target.lastElementChild.innerHTML = insertText;
    return true
  }
 });

What is the best way to implement this functionality for new entries anywhere in the list not just the end? 在列表中的任何位置实现此功能的最佳方法是什么,而不仅仅是结束? Open to Jquery solutions 打开Jquery解决方案

example jsfiddle 示例jsfiddle

You could use a MutationObserver to detect when a child is added to your list, and then update the previousSibling to wrap it in a <span> : 您可以使用MutationObserver来检测子项何时添加到列表中,然后更新previousSibling以将其包装在<span>

 function subscriber(mutations) { mutations.forEach(mutation => { mutation.addedNodes.forEach(node => { const prev = node.previousSibling; if (prev) { prev.innerHTML = `<span>${prev.innerHTML.replace(/<br>$/, '')}</span>`; } }); }); } const observer = new MutationObserver(subscriber); observer.observe(document.querySelector('ol[contenteditable]'), { childList: true }); 
 .editor span::after { content: '😀'; } 
 <ol contenteditable class="editor"> <li>First li</li> </ol> 

You could bind your logic to the last li , and perform your logic from the events it emits. 您可以将逻辑绑定到最后一个li ,并根据它发出的事件执行逻辑。

 $('.editor .last-item') .on('click', function(e){ // clear out the html so the user can just type e.target.innerHTML = ''; }) .on('keydown', function(e){ if (e.keyCode === 13) { // ignore the enter key so it doesn't insert a new like e.preventDefault(); // create the new li before the last one $('<li>'+ e.target.innerHTML.trim() +'</li>').insertBefore(e.target); // clear out the html so the user can keep entering items e.target.innerHTML = ''; } }) .on('blur', function(e){ // if the user leaves the field, change it back to the instructional text e.target.innerHTML = 'Click to enter New List Item'; }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <ol class="editor"> <li class="last-item" contenteditable=true>Click to enter New List Item</li> </ol> </div> 

Instead of taking action when an edit happens, you could set an interval that will modify the html of the li elements as desired. 您可以设置一个间隔,根据需要修改li元素的html,而不是在编辑发生时执行操作。

setInterval(function(){
    $('.editor' ).find('li').each(function(){
    if ($(this).html().indexOf('span')==-1){
            $(this).html('<span>' + $(this).html() + '</span>');
    }
  });
}, 200);

hello you might wanna check this let me give you a heads up. 你好,你可能想检查这个,让我给你一个抬头。 instead of using the 而不是使用

lastchild.innerHTML

replace it with 用它替换它

nextSibling.innerHTML

like this 像这样

 $('.editor' ).on('keydown .editable', function(e){ if ( e.keyCode === 13 ) { var insertText = "<span>"+e.target.lastElementChild.innerText+"</span>"; e.target.nextSibling.innerHTML = insertText; return true } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <ol contenteditable=true class="editor"> <li><br></li> </ol> </div> 

 $('.editor' ).on('keydown .editable', function(e){ if ( e.keyCode === 13 ) { var insertText = "<span>"+e.target.lastElementChild.innerText+"</span>"; e.target.nextSibling.innerHTML = insertText; return true } }); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM