2012-12-28 29 views
8

在我的網頁。我有一個整個的div不可選擇的,與這個CSS的幫助使文本可選擇

.unselectable { 
    -moz-user-select: -moz-none; 
    -khtml-user-select: none; 
    -webkit-user-select: none; 

    /* 
    Introduced in IE 10. 
    See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/ 
    */ 
    -ms-user-select: none; 
    user-select: none; 
} 

現在,我得到內部的H2標籤(這是我從計算器它的自我了)這是無法選擇的div.And我希望h2成爲可選擇的。有什麼辦法可以實現這個簡單。

我在我的問題中忘記了一些東西。我在我的代碼中得到了三個h2我需要特定的一個可選,所以我添加了一個類到這個h2並嘗試了一些這樣的事情.unselectable:not(.class-of-h2)(我從下面的答案中得到)。但它不工作

回答

17

使用您的h2元素

.unselectable h2{ 
-moz-user-select: text; 
-khtml-user-select: text; 
-webkit-user-select: text; 
-ms-user-select: text; 
user-select: text; 
} 

See demo

+0

我不知道這是否是正確的答案,但這件事絕對爲我工作。 – Athul

+0

是的,你可以簡單地重寫父元素上的設置。 [user-select'上的MDN頁面](https://developer.mozilla.org/en-US/docs/CSS/user-select?redirectlocale=en-US&redirectslug=CSS%2F-moz-user-select)有點讓人誤解:通常'none'允許在內部元素中覆蓋,但Firefox有不同的設置:'none'不允許覆蓋,'-moz-none'不會。 –

1

可以使用

 .unselectable:not(h2:nth-child(2)) { 
     //your data 
     } 

可以從你的選擇
這個清單中排除的元素可以讓你的整個DIV不可選擇的,除了h2元素。

+0

:我已經忘了小號在我的問題中的東西。我在我的代碼中得到了三個h2我需要特定的一個可選,所以我給這個h2添加了一個類,並嘗試了一些像這樣的事情。.unselectable:not(.class-of-h2)。but not not working – Athul

+0

你可以使用.unselectable:不(h2:nth-​​child(2))這樣的東西.. – sourcecode

2

只需添加以下CSS

.unselectable h2 { 
    -moz-user-select: text; 
    -khtml-user-select: auto; 
    -webkit-user-select: auto ; 
    -ms-user-select: auto; 
    user-select: auto; 
} 

demo