Using JavaScript to implement HTML text is not selectable

Source: Internet
Author: User

How to use JS to make the text in HTML not selectable? The first way to think about this is to use a CSS selector, as follows:

-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;- Ms-user-select:none;user-select:none;

However, this is not compatible with the old browser, so this article will discuss how to use JS to implement, and compatible with all browsers.

The first thing to think about is:

<!doctype html>


This makes it possible to complete HTML text without the option, and if you are using jquery you can also extend the jquery plugin in a way that implements:


<!doctype html>
Or:

(function ($) {$.fn.disableselection = function () {    return This.each (function () {        if () {typeof This.onselectstart ! = ' undefined ') {            This.onselectstart = function () {return false;};        } else if (typeof This.style.MozUserSelect!) = ' undefined ') {            this.style.MozUserSelect = ' none ';        } else {            This.onmousedown = function () {return false;};        }    });};}) (jQuery); $ (document). Ready (function () {    $ (' label '). Disableselection ();    Or to make everything unselectable    $ (' * '). Disableselection ();});

OK, so you can basically be compatible with all browsers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.