2012-12-04 120 views
12
#btnLeft{ 
    width:40px; 
    padding:2px 5px 2px 5px; 
    background-color: #20a0a0; 
    border: thin solid #099; 
    border-radius:7px; 
    text-align:center; 
    font-family:"Futura Md BT"; 
    font-size:large; 
    color:#FFF; 
} 

這是一個css按鈕。
當用戶意外地進行雙擊而不是一個單一的按鈕時,該按鈕內的文本被選中,即 - 改變其背景顏色。
如何防止這種雙擊選擇?如何防止雙擊選擇文本?

+0

這個問題的重複? http://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting – Michelle

+1

@Michelle,什麼是您的鏈接接受的解決方案?另外,我不想阻止在頁面正文部分選擇文本,只是在按鈕上。 – Alegro

+0

@ Michelle鏈接上的「已接受」解決方案是獲得更多投票的解決方案。爲什麼該CSS代碼會阻止用戶從頁面的其他部分選擇文本?你把它放在你的#btnLeft {}上,它不會干擾文檔的其他部分。 –

回答

39

儘量小的jQuery

$(element).mousedown(function(){ return false; }) 

與CSS嘗試

-webkit-user-select: none; 
-moz-user-select: none; 
-khtml-user-select: none; 
-ms-user-select: none; 
+1

NullPointer,非常感謝。解決了 – Alegro

+1

CSS爲我工作,謝謝! – Leo

6

如果使用<button><a>標籤和包括href屬性,那麼該行爲應當減輕。而它更多的語義:)

+0

在某些情況下,這不可能像使用標籤內的複選框:) –