javascript - 如果内容可编辑 div 位于光标位置下方,如何删除它?

标签 javascript jquery html removeclass

每当单击该类或在其上按下按键时,我都会尝试从此 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);
}

示例: http://jsfiddle.net/nickg1/ttMJW/1/

关于javascript - 如果内容可编辑 div 位于光标位置下方,如何删除它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21766779/

相关文章:

javascript - 自定义 jquery 或 javascript 从字符串扩展中删除特殊字符

html - 如何在 HTML 中为某些浏览器设置缩放百分比?

css - 如何水平对齐表格和图像

javascript - img src变化时给图片添加Slidein转场效果?

javascript - GruntJs 任务问题

javascript - 在不查看特定元素的情况下使用 javascript 获取和更改 CSS 属性

javascript - 在数组文字中使用构造函数创建对象

jQuery 动画故障

html - 打印时缩放 HTML 以适合一页

jquery - 删除div onclick(使用remove)