如何使用键(ctrl +)(jQuery)触发事件

时间:2011-07-23 20:45:47

标签: javascript jquery events

如果按 Ctrl 键加上++键(放大),如何使用jQuery触发事件。

key = Ctrl ++

3 个答案:

答案 0 :(得分:5)

试试这个

$(window).keypress(function(e){

   if((e.which == 61 && e.ctrlKey) || (e.which == 43 && e.ctrlKey)){
       //Ctrl + "+" is pressed, 61 is for =/+ anr 43 is for Numpad + key
   } 

});

答案 1 :(得分:2)

绑定到Ctrl+I的示例。请注意,您无法覆盖默认浏览器行为,因此保留了许多Ctrl+(letter)快捷方式(Ctrl + T =新标签,Ctrl + N =新窗口,Ctrl + P =打印等...)

$(window).keydown(function(e){
  if(e.which == 17)
      $(window).bind('keydown.ctrlI', function(e){
          if(e.which == 73){
              e.preventDefault();
              alert('CTRL+I');
          }
      });
});

$(window).keyup(function(e){
  if(e.which == 17)
      $(window).unbind('keydown.ctrlI');
});

答案 2 :(得分:0)

// the element at which you are firing the event
var div = $('#foo');

// the event handler
div.bind('paint', function() {
    $(this).addClass('painted');
});

$(window).keydown(function(e) {
    // if CTRL + + was pressed
    if ( e.ctrlKey && e.which === 187 ) {
        // trigger the event
        div.trigger('paint');
    }
});

现场演示: http://jsfiddle.net/NMYJW/