每当单击该类或在其上按下按键时,我都会尝试从此 HTML 中删除类 .words
。
<div id="content" contenteditable="true"> Here are <span class="words" id="whatever">some</span> words </div>
我已经尝试过
$('.words').click(function() { $(this).remove(); });
和more ,除非我单击另一个单词并使用左键或右键循环 .words
(因为它是内容可编辑的),否则它会起作用。我希望当光标移至类(class)上方时将其删除。谢谢。
最佳答案
类似这样的吗?
var words = $('.words')[0]
$('#content').keyup(function(){
if (words == getSelectionStart()){
$(words).remove()
}
})
function getSelectionStart() {
var node = document.getSelection().anchorNode;
return (node.nodeType == 3 ? node.parentNode : node);
}
关于javascript - 如果内容可编辑 div 位于光标位置下方,如何删除它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21766779/