通過CSS規則禁止選中文字的代碼執行個體

來源:互聯網
上載者:User
禁止選中文字在某些情境下還是有必要的,比如不想讓他人複製自己的文章之類的。這時候我們可以通過使用CSS+JS來解決這一問題。另外,這邊要指出的是,user-select目前還不是W3C的正式標準,各個瀏覽器都是以私人屬性的方式提供支援。

文法

Formal syntax: none | text | all | element

代碼如下:

(-prefix-)user-select: none;(-prefix-)user-select: text;(-prefix-)user-select: all;(-prefix-)user-select: element;

代碼如下:

.row-of-icons {-webkit-user-select: none; /* Chrome all / Safari all */-moz-user-select: none; /* Firefox all */-ms-user-select: none; /* IE 10+ */</p> <p> /* No support for these yet,use at own risk */-o-user-select: none;user-select: none;}

IE相容性

目前在IE 10以及IE 10以上版本的瀏覽器可以使用-ms-user-select這個規則,但在更早版本的IE,我們只能通過javascript實現禁止選中文本:

代碼如下:

$(el).attr('unselectable','on').css({'-moz-user-select':'-moz-none','-moz-user-select':'none','-o-user-select':'none','-khtml-user-select':'none', /* you could also put this in a class */'-webkit-user-select':'none',/* and add the CSS class here instead */'-ms-user-select':'none','user-select':'none'}).bind('selectstart', function(){return false;});

相關文章

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.