Javascript 删除键控功能上的选定(高亮显示的元素)
我有两个功能:Javascript 删除键控功能上的选定(高亮显示的元素),javascript,jquery,selection,Javascript,Jquery,Selection,我有两个功能: var $mainEdit= $("#main-edit"); function getSelText() { var txt = ''; if (window.getSelection) { txt = window.getSelection(); } else if (document.getSelection) {
var $mainEdit= $("#main-edit");
function getSelText()
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
else return;
return $("#clipboard").val(txt);
}
$mainEdit.mouseup(function(){
$("#clipboard").val("");
getSelText();
}).mousedown(function(){
$("#clipboard").val("");
getSelText();
});
我想做的是,在keyup事件中…高亮显示的元素将被删除
如果我有这个html:
<span>a</span>
<span>b</span>
<span>c</span>
a
B
C
并突出显示a和b,在keyup事件中,前两个跨距将被删除。这里有一个用于删除选定内容的跨浏览器功能:
function deleteSelected() {
if (window.getSelection()) {
window.getSelection().deleteFromDocument();
} else if (document.selection) {
document.selection.clear();
}
}
将其连接到keyup
事件:
$mainEdit.keyup(deleteSelected);