disable text selection while pressing 'shift'

前端 未结 5 940
我寻月下人不归
我寻月下人不归 2020-12-08 09:44

I\'m implementing item selection functionality in javascript (using jQuery). item is a li that contains some html.
the user can click on one item (makes it select

5条回答
  •  半阙折子戏
    2020-12-08 10:17

    I have an application like that. Trick is, I wanted to allow selection, but I also wanted Ctrl-click and Shift-click for selecting items.

    What I found was that everyone but IE allows you to beat this by canceling the mousedown event, and in IE what works best is to temporarily disable onselectstart:

    $("#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);
            }
        }
    });
    

提交回复
热议问题