我在JavaScript中實現了項目選擇功能(使用jQuery)。 item是包含一些html的li。
用戶可以點擊一個項目(使其選中),然後按住Shift鍵並單擊另一個項目以選擇其中的所有項目。這基本上可以正常工作,但是shift-click還會選擇(高亮)項目內出現的文本(用於輪班點擊的基本瀏覽器功能)。有什麼辦法可以禁用它嗎?禁用按住'shift'時的文本選擇
回答
嘗試組合的JavaScript和CSS以防止選擇首先:
$('li').attr('unselectable', 'on'); // IE
css(對於瀏覽器不是IE):
li {
user-select: none; /* CSS3 (little to no support) */
-ms-user-select: none; /* IE 10+ */
-moz-user-select: none; /* Gecko (Firefox) */
-webkit-user-select: none; /* Webkit (Safari, Chrome) */
}
試試這個後移+點擊...
document.getSelection().removeAllRanges();
如果這還不夠有效,你可能也必須取消焦點的時候事件......
window.onload = function() {
document.onselectstart = function() {
return false;
}
}
+1取消onselectstart。 – 2011-02-03 22:33:59
我有這樣的應用程序。竅門是,我想允許選擇,但我也想按住Ctrl並按住Shift鍵點擊選擇項目。
我發現那是什麼每個人,但IE瀏覽器允許您通過取消鼠標按下事件擊敗這一點,並在IE什麼工作最好是暫時焦點的時候禁用:
$("#id").mousedown(function (e) {
if (e.ctrlKey || e.shiftKey) {
// For non-IE browsers
e.preventDefault();
// For IE
if ($.browser.msie) {
this.onselectstart = function() { return false; };
var me = this; // capture in a closure
window.setTimeout(function() { me.onselectstart = null; }, 0);
}
}
});
這是我正在尋找的祕密。在Chrome上瞬間獲得選擇,並且在Safari上也需要添加'document.getSelection()。removeAllRanges();' – prototype 2014-06-21 02:43:15
如果你在你的錶行的複選框(項目),只要您做出選擇,您可以簡單地將焦點設置到該行的複選框。關注複選框應該擺脫瀏覽器選擇。您還可以專注於文本框或其他表單元素。 對於JQuery,
$('tr').bind('click', function(e) {
// If shift key was down when the row was clicked
if (e.shiftKey) {
// Make the row selected
$(this).parent().children().slice($(last).index(), $(this).index()+1).addClass('selected').find('input:checkbox').attr('checked', true);
// Now focus on the checkbox within the row
$('input:checkbox', $(this)).focus();
}
});
- 1. 單擊時按住Shift?
- 2. 按住Shift鍵的DataGrid中的多個範圍選擇(Flex 3)
- 3. 禁用文本選擇onclick按鈕
- 4. 選擇文本在一個文本給定範圍內(如如果用戶按住shift和箭頭)
- 5. wxPyhton通過按住Shift鍵在wxGrid中選擇更多行
- 6. 右鍵單擊時按住Shift鍵?
- 7. JQuery Flot:在按住Shift鍵時平移
- 8. 檢測何時按住shift鍵
- 9. 按住Shift + F10 w(h)?
- 10. 雙擊時禁用文本選擇
- 11. 文本選擇禁用
- 12. 禁用文本選擇UITextView
- 13. 禁用基於單選按鈕選擇的文本框
- 14. CGEventPost - 按住一個按鍵(shift)
- 15. 用Ctrl選擇多個HTML錶行+單擊並按住Shift並單擊
- 16. iOS:在點擊並按住單元格後禁用UITableView選擇
- 17. Sublime Text 3禁用SHIFT +刪除複製選定文本
- 18. 在富文本框中選擇文本時禁用滾動(C#)
- 19. 禁用HTML元素的文本選擇
- 20. 的javascript:禁用文本選擇
- 21. 如何禁用文本框的選擇
- 22. 當用戶選擇選項時禁用文本輸入
- 23. Eclipse中不再允許按住Shift-F
- 24. 綁定Alt + Shift鍵按住Ctrl鍵
- 25. Jquery event.preventDefault沒有按住shift鍵點擊
- 26. 當用戶沒有選擇文件時,JAVA禁用按鈕
- 27. 使用VBA打開由mdw文件保護的ms-access數據庫時,按住SHIFT鍵時按下按鈕?
- 28. 選擇按鈕的文本
- 29. 在PhoneGap中禁用文本選擇
- 30. 在gwt中禁用文本選擇
請注意,unselectable =「on」也必須位於所有子節點上(例如div,p)。即使是複選框小部件也可以按住Shift鍵單擊。 – 2014-04-16 18:39:00
這沒有解決「按下換檔」部分。請參閱@Anthony Mills對此的答案。 – ahlexander 2018-01-16 09:26:37