This article describes how JS clears the selection content. Share it for your reference. The specific analysis is as follows:
Today, I was doing a DIV drag effect and found that the text on the page will be selected when dragging, so I found the relevant information on JS clearing the selected content.
In the results obtained, it is found that in Google, Firefox, and Opera browsers, the window object has a getSelection property, but not in IE. The document object in IE has a selection attribute, so clearing the selected content on the page can be solved.
In Google, Firefox, and Opera browsers, we can easily clear the selected content through window.getSelection().removeAllRanges(), and in IE we can clear the selected content through document.selection.empty().
So we can write this:
var clearSlct= "getSelection" in window ? function(){ window.getSelection().removeAllRanges();} : function(){ document.selection.empty();};"getSelection" in window is used to determine whether the window object contains the getSelection property. If true, it means that the current browser supports getSelection, that is, the browser is a non-IE browser, and vice versa.
If we want to prohibit the user from selecting content in the page, we can do this:
//Prevent the mouse from selecting content (clear the selected content when the mouse is released) window.onmouseup=function(){ clearSlct();} //Prevent the content from selecting content through the keyboard (clear the selected content when the key is released) window.onkeyup=function(){ clearSlct();} //Use jQuery method $(window).on("mouseup keyup",function(){ clearSlct();});I hope this article will be helpful to everyone's JavaScript programming.