2015-08-28 51 views
0

使用select元素在我的應用程序,我們使用這個模式很多:停用文本選擇,並與Firefox 40

<select name="select" onfocus=this.select; onClick=this.focus();> 
 
    <option value="value1" selected>Value 1</option> 
 
    <option value="value2">Value 2</option> 
 
</select> 
 

 
<script> 
 
document.onmousedown = new Function("return false"); 
 
</script>

document.onmousedown = new Function("return false");旨在防止頁面文本選擇。不幸的是,它也阻止了mousedown事件傳播到select元素。我們使用的workarround是回調onfocus=this.select; onClick=this.focus();

我的問題是,它不適用於Firefox 40:單擊選擇框什麼也不做。 (與39正常工作)

我可以刪除行document.onmousedown = new Function("return false");來修復選擇框,但它會允許在頁面中選擇文本。

我的問題是:我有哪些選項可以同時使用Firefox 40和工作選擇框?

+0

你不能只使用CSS? '-moz-user-select:none;用戶選擇:無;(和任何必需的供應商前綴) – CodingIntrigue

+0

@Rraham Thx,我將重點關注如何處理select元素以搜索其他方式...... –

回答

1

您可以使用CSS。

.noselect { 
    -webkit-touch-callout: none; 
    -webkit-user-select: none; 
    -khtml-user-select: none; 
    -moz-user-select: none; 
    -ms-user-select: none; 
    user-select: none; 
} 

然後應用noselect類(或任何你想將它命名)到其文本你不想爲可選擇的任何元素。

請參閱this question討論在不同瀏覽器中支持這些不同css屬性。