简体   繁体   中英

jquery draggable the entire div

When using draggable, how can I grab the whole div and not just the words in the div?

<div id='something'>
   <ul>
      <li class='draggable' id='frank'>Frank</li>
      <li class='draggable' id='Joe'>Joe</li>
  </ul>
</div>

<div id='somethingElse'>
   <div class='dragme' id='yes'>Yes</div>
   <div class='dragme' id='no'>no</div>
</div>

  $(function() {
      $( "li" ).draggable();
      $(".dragme").draggable();
  });

Here is my JSFiddle

Fiddle

Add the draggable class to the DIVs you want to drag, and remove it from the LI elements:

<div class='draggable' id='something'>
    <ul>
        <li id='frank'>Frank</li>
        <li id='Joe'>Joe</li>
    </ul>
</div>
<br>
    <br>
        <div class='draggable' id='somethingElse'>
            <div id='yes'>Yes</div>
            <div id='no'>no</div>
        </div>

Then call .draggable on everything that has the draggable class.

$(function() {
    $( ".draggable").draggable();
});

Make the containers draggable, not the list items:

$("#something, #somethingElse").draggable();

jsFiddle example

Just call .draggable() on the top-level element which you want to be draggable. I would recommend using a special class (see Tony Hinkle's answer), but without changing your HTML code, it would be:

$(function() {
  $( "#something, #somethingElse" ).draggable();
});

Or if you want to select containers based on the fact that they contain elements of specific class, then use .has() selector:

$(function() {
  $( "div:has(.dragme), div:has(.draggable)" ).draggable();
});
$("#something").draggable();
$("#somethingElse").draggable();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM