Html text using javascript is not optional

Source: Internet
Author: User

Html text using javascript is not optional

How can I use JavaScript to make the text in html not optional? The first method is to use the 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;

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

The first thing that comes to mind is:

            SO question 2310734        <script>            window.onload = function() {                var labels = document.getElementsByTagName('label');                for (var i = 0; i < labels.length; i++) {                    disableSelection(labels[i]);                }            };            function disableSelection(element) {                if (typeof element.onselectstart != 'undefined') {                    element.onselectstart = function() { return false; };                } else if (typeof element.style.MozUserSelect != 'undefined') {                    element.style.MozUserSelect = 'none';                } else {                    element.onmousedown = function() { return false; };                }            }        </script>                Try to select this    


In this way, html text is not optional. If you are using jQuery, you can also extend the JQuery plug-in to implement it:


            SO question 2310734 with jQuery        <script src="http://code.jquery.com/jquery-latest.min.js"></script>        <script>            $.fn.extend({                 disableSelection: function() {                     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; };                        }                    });                 }             });            $(document).ready(function() {                $('label').disableSelection();                        });        </script>                Try to select this    

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();});

Okay, so that it can be basically 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.