跨浏览器的 document.selection.clear() 和 empty()实现

function RemoveSelection () {
            if (window.getSelection) {  // all browsers, except IE before version 9
                var selection = window.getSelection ();
                selection.deleteFromDocument ();

                    /* The deleteFromDocument does not work in Opera.
                        Work around this bug.*/
                if (!selection.isCollapsed) {
                    var selRange = selection.getRangeAt (0);
                    selRange.deleteContents ();
                }

                    // The deleteFromDocument works in IE,
                    // but a part of the new content becomes selected
                    // prevent the selection
                if (selection.anchorNode) {
                    selection.collapse (selection.anchorNode, selection.anchorOffset);
                }
            } 
            else {
                if (document.selection) {    // Internet Explorer
                    document.selection.clear ();
                }
            }
   }

function EmptySelection(){
   if(document.selection){
       document.selection.empty();
   }else{
     var selection = document.getSelection();
     selection.removeAllRanges();
  }
}


你可能感兴趣的:(跨浏览器的 document.selection.clear() 和 empty()实现)