DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
用 CSS 規則禁止選取文字的實現代碼
編輯:CSS詳解     

語法

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 相容性

目前 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 事件來達成相同效果。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved