Tôi sợ bạn không thể ngăn chặn sự lựa chọn riêng của mình là "hành vi tự nhiên" của trình duyệt, nhưng bạn có thể xóa các lựa chọn ngay sau khi nó được thực hiện:
<script type="text/javascript">
document.ondblclick = function(evt) {
if (window.getSelection)
window.getSelection().removeAllRanges();
else if (document.selection)
document.selection.empty();
}
</script>
Sửa: cũng để ngăn chặn việc lựa chọn toàn bộ theo "ba lần nhấp", đây là mã được yêu cầu:
var _tripleClickTimer = 0;
var _mouseDown = false;
document.onmousedown = function() {
_mouseDown = true;
};
document.onmouseup = function() {
_mouseDown = false;
};
document.ondblclick = function DoubleClick(evt) {
ClearSelection();
window.clearTimeout(_tripleClickTimer);
//handle triple click selecting whole paragraph
document.onclick = function() {
ClearSelection();
};
_tripleClickTimer = window.setTimeout(RemoveDocumentClick, 1000);
};
function RemoveDocumentClick() {
if (!_mouseDown) {
document.onclick = null;
return true;
}
_tripleClickTimer = window.setTimeout(RemoveDocumentClick, 1000);
return false;
}
function ClearSelection() {
if (window.getSelection)
window.getSelection().removeAllRanges();
else if (document.selection)
document.selection.empty();
}
Live test case.
Phải là trình duyệt chéo, vui lòng báo cáo mọi trình duyệt khi trình duyệt không hoạt động.
Nguồn
2010-11-07 11:39:46
Tại sao bạn muốn thực hiện việc này? –
Không phải quyết định của tôi. Anh chàng tôi đang mã hóa vì muốn nó như thế. Có thể vì trang là một phần của tên người duyệt và một số nút (divs có các sự kiện nhấp chuột) giúp tăng/giảm giá trị thu hút người dùng lên gấp đôi vì nhanh hơn nhiều so với việc sử dụng chúng nhiều lần. Và nếu điều đó chọn công cụ nó gây phiền nhiễu và có vẻ lạ. – ThiefMaster
http://stackoverflow.com/questions/880512/prevent-text-selection-after-double-click – Aristoteles