当按下CTRL ALT时,活动jQuery函数



我想在jQuery中(and)jquery中的 draggableresizable函数在jQuery中(and)释放时按下ctrl alt按钮,并禁用它,我写了此代码

>

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Draggable - Default functionality</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <style>
    #para {
      width: 150px;
      height: 150px;
      padding: 0.5em;
    }
    
    .ui-resizable-helper {
      border: 2px dotted #00F;
    }
  </style>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
    $(function(e) {
      if (e.ctrlKey || e.altKey) {
        $("#para").draggable();
        $("#para").resizable({
          helper: "ui-resizable-helper"
        });
      }
    });
  </script>
</head>
<body>
  <div id="para" class="ui-widget-content">
    <h1 contenteditable>Header</h1>
    <hr />
    <p contenteditable>paragraph</p>
  </div>
</body>
</html>

完成此代码后,我尝试在浏览器中按CTRL ALT,但它不起作用,我删除了if (e.ctrlKey || e.altKey)逻辑部分,并且成功工作,但是当我替换逻辑语句时,工作

如何解决此问题?

您需要一个事件处理程序来捕获关键。

您可以将插件初始化为disabled,然后在按键按键时enable,然后在释放键时再次disable

 $(function() {
    $("#para").draggable({
        disabled : true
    });
    $("#para").resizable({
        helper   : "ui-resizable-helper",
        disabled : true
    });
    $(document).on({
        keydown : function(e) {
            if (e.ctrlKey && e.altKey) {
                $("#para").draggable( "enable" );
                $("#para").resizable( "enable" );
            }
        }, 
        keyup : function() {
            $("#para").draggable( "disable" );
            $("#para").resizable( "disable" );
        }
    });
});
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Draggable - Default functionality</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <style>
    #para {
      width: 150px;
      height: 150px;
      padding: 0.5em;
    }
    
    .ui-resizable-helper {
      border: 2px dotted #00F;
    }
  </style>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
  <div id="para" class="ui-widget-content">
    <h1 contenteditable>Header</h1>
    <hr />
    <p contenteditable>paragraph</p>
  </div>
</body>
</html>

最新更新