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+ */
/* No support for these yet, use at own risk */
-o-user-select: none;
user-select: none;
}
目前 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;
});
透過 user-select 及其各家瀏覽器前綴版本,可以防止使用者選取(複製)指定元素的文字內容;對於不支援此屬性的舊版 IE,則需搭配 unselectable 屬性與 selectstart 事件來達成相同效果。