开发者

Jquery selectable only with shortcut

I want to let the user select when the shift key is held.

$("#div").selectable({
        start: function(st) {
            $(window).keydown(function(e){
                if(!e.shiftKey){
                    st.stopPropagation();
                }
        开发者_如何学Go    });
        });

no?


You can shorten down your code to be much simpler by using the .shiftKey property on the event directly (it's present in the mousedown event too), like this:

$("#div").mousedown(function(e){
   if(e.shiftKey) return;
   e.stopImmediatePropagation();
   return false;
}).selectable();

You can test it out here.


for those who need it or something similar, this worked well for me:

    var shift = false;

    $(window).keydown(function(e){
        if(e.shiftKey){
    shift = true;
        }
    })
    .keyup(function(e){
        if(!e.shiftKey){
            shift = false;
        }
    });

    $("#div")
    .mousedown(function(e){
       if(!shift){
         e.stopImmediatePropagation();
         return false;          
       }
    })
    .selectable();


$(window).keydown(function(e){
  if(!e.shiftKey){
     $("#div").selectable({
        start: function(st) {
        st.stopPropagation();
          //your code here
        });

     }
});

if that doesn't work try use document instead of window or 'body'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜