我想在按下ctrl + alt按钮的时候激活jquery中的draggable
和resizable
函数,并在它被释放时禁用,我写了这段代码
<!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)
逻辑部分并且它成功运行但是当我替换那里的逻辑语句时它不起作用
我该如何解决这个问题?
答案 0 :(得分:1)
您需要一个事件处理程序来捕获按键。
您可以将插件初始化为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" );
}
});
});
&#13;
<!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>
&#13;