Home  >  Article  >  Web Front-end  >  Javascript implementation code to cancel text selection_javascript skills

Javascript implementation code to cancel text selection_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:16:361342browse

Cancel selected text in javascript, now compatible with all major browsers:

Copy code The code is as follows:

if (document.selection) {
document.selection.empty();
} else if (window.getSelection) {
window.getSelection().removeAllRanges();
}

For unselecting the text in the text box (input, textarea), this method will have some problems.

But there is also a way, record the value in the text box, and then Clear it and reassign it. The method is a bit confusing, but it is compatible with all browsers.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn