带有图标的jQuery可拖动DIV

Iva*_*der 0 jquery jquery-ui draggable

我想制作一些可拖动的DIV与jQuery'可拖动,但那些不应该是可拖动的,但只有在每个DIV顶部的一个小图标的帮助下,所以我只能移动它,如果我点击并拖动它DIV上方的图标,而不是每当我点击并拖动div时拖动.

提前致谢

Pra*_*man 6

使用手柄:

$( "#draggable" ).draggable({
  handle: "p"
});
Run Code Online (Sandbox Code Playgroud)

片段

$(function() {
  $( "#draggable" ).draggable({ handle: "p" });
  $( "#draggable2" ).draggable({ cancel: "p.ui-widget-header" });
  $( "div, p" ).disableSelection();
});
Run Code Online (Sandbox Code Playgroud)
#draggable, #draggable2 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
#draggable p { cursor: move; }
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="https://jqueryui.com/resources/demos/style.css">
<div id="draggable" class="ui-widget-content">
  <p class="ui-widget-header">I can be dragged only by this handle</p>
</div>

<div id="draggable2" class="ui-widget-content">
  <p>You can drag me around&hellip;</p>
  <p class="ui-widget-header">&hellip;but you can't drag me by this handle.</p>
</div>
Run Code Online (Sandbox Code Playgroud)