如何获得拖动元素的ID并在删除时绑定?

时间:2012-10-30 13:06:54

标签: javascript jquery

大家好我怎么能得到拖动元素的Id并绑定我丢弃元素的id 这就是我为拖拉而做的事情

        var i = 1;
       var z = 1;
       $(document).ready(function () {
           $("button[id='columnadd']").click(function () {
               // create the element
               var domElement = $('<div id="shoppingCart' + i++ + '" class="shoppingCart"><h2 class="ui-widget-header">Add Menu Items Here <img src="../Images/delete.png" id="shoppingCart' + z++ + '" style="float: right; cursor:pointer;" onclick="removecolumn(this.id)"/></h2><aside class="ui-widget-content" id="droppable"><ol><li class="placeholder">Add your items here</li></ol></aside></div>');
               var holder = "#columnholder";
               $("#columnholder").append(domElement);
               //$(this).after(domElement);

               // at this point you have domElement in your page

               // make it droppable
               domElement.find("ol").droppable({
                   activeClass: "ui-state-default",
                   accept: '.small_box li',
                   greedy: true,
                   drop: function (event, ui) {                          
                       makeItDroppableToo(event, ui, this);                                             
                   }
               });
           });

           function makeItDroppableToo(e, ui, obj) {                  
               $(obj).find(".placeholder").remove();
               var id = this.id;
               alert(id);
               var placeholder = $("<ol><li>Add Sub Menu here</li></ol>");
               $("<li></li>").append('<a  draggable="true" droppable=true>' + ui.draggable.text() + '</a>').append(placeholder).appendTo($(obj));

               // this is the same as the previous one
               placeholder.droppable({
                   greedy: true,
                   // put options here
                   drop: function (event, ui) {
                       makeItDroppableToo(event, ui, this);
                   }
               });
           }

           $(".small_box li" ).draggable({
               appendTo: "body",
               helper: "clone"
           });
       });

这是我的HTML从我拖动的地方

         <div class="top-header"><span class="heading">Menu Builder</span><button id="preview" onclick="savemenu()" >Save</button><button id="columnadd" >Add Column</button></div>
<div id="columnholder" class="column-holder"></div>
<div class="clear"></div>
<div class="menu-items">    
<aside class="small_box">
     <div class="menu-item-header"><h4>BRANDS</h4></div>
      <ul>
             <li id ="1"><a id ="1001" class="" href="#">Brand1</a></li>
            <li id ="2"><a id ="1002" href="#">Brand2</a></li>
            <li id ="3"><a id ="1003" href="#">Brand3</a></li>
            <li id ="4"><a id ="1004" href="#">Brand4</a></li>
            <li id ="5"><a id ="1005" class="" href="#">Brand5</a></li>
            <li id ="6"><a id ="1006" href="#">Brand6</a></li>
            <li id ="7"><a id ="1007" href="#">Brand7</a></li>
            <li id ="8"><a id ="1008" href="#">Brand8</a></li>
     </ul>
    </aside>

这里我将拖动元素,当我这样做,我想得到id,当我绑定拖动元素这里

           $("<li></li>").append('<a  draggable="true" droppable=true>' + ui.draggable.text() + '</a>').append(placeholder).appendTo($(obj));

我想将该ID绑定到,可以任何人告诉我该怎么做

1 个答案:

答案 0 :(得分:1)

在放置处理程序中,您可以使用ui.draggable.attr('id')来获取被拖动元素的ID。

...
drop: function(ev, ui) {

   /*
   // get id of dragged element   
   var draggedID = ui.draggable.attr('id');

   // bind it
   #('#'+draggedID).bind(...); // bind your events here
   */

   // why get the id, when you can bind it directly
   // will work with elements without an id too (credit to Rory McCrossan)
   ui.draggable.bind(...);
},
...