onselectstart和onselect的使用
1.onselectstart
這個事件主要是用於禁止選擇網頁中的文字。代碼爲:
document.onselectstart =function(){
return false;
}
另外ff/opera不支持此事件,ff可以用css控制:css: body { -moz-user-select: none; }。webkit瀏覽器可以使用“-khtml-user-select”,當然也可以使用onselectstart事件來阻止用戶選定元素內文本。
注意此事件不支持對input和textarea無效。
2.onselect
此事件在選擇textarea或input內的內容後觸發。因此只有input和textarea標籤支持。例如:
<input type = 'text' id = 'input>
<script>
var input = document.getElementById('input');
input.onselect = function(){
alert("被觸發");
}
</script>
3.use-select: none;
.className{
use-select: none;
}