2013-05-02 125 views
0

我在下面的代碼如下。它有一個更大的div內的div。我只想讓「#set」div移動,而不是其他div。但現在用戶可以移動div內的任何內容。我怎樣才能讓它只移動大股東「集」?Jquery所有div的移動,但只需要一個移動

頁眉:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> 

<style> 
    #set { clear:both; float:left; width: 368px;} 
    p { clear:both; margin:0; padding:1em 0; } 
</style> 

<script> 
$(function() { 
    $("#set div").draggable({ 
    stack: "#set div", 
     stop: function(event, ui) { 
      var pos_x = ui.offset.left; 
      var pos_y = ui.offset.top; 
      var need = ui.helper.data("need"); 

      console.log(pos_x); 
      console.log(pos_y); 
      console.log(need); 

      //Do the ajax call to the server 
      $.ajax({ 
       type: "POST", 
       url: "updatecoords.php", 
       data: { x: pos_x, y: pos_y, need_id: need} 
      }) 
     } 
    }); 
}); 
</script> 




身體:

<div id="set"> 

<? 

$getcoords = mysql_query("SELECT * FROM coords WHERE needid=6"); 
$rows = mysql_fetch_assoc($getcoords); 
$x = $rows['x']; 
$y = $rows['y']; 

echo " 

<div style='width: 175px; height: 350px; padding: 0.5em; float: left; margin: 0 10px 10px 0; background: white; position:fixed;left:".$x."px; top:".$y."px;' data-need='6' class='column'> 

    <div class='portlet'> 
    <div class='portlet-header'>Contacts</div> 
    <div class='portlet-content'>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div> 
    </div> 

    <div class='portlet'> 
    <div class='portlet-header'>Notes</div> 
    <div class='portlet-content'>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div> 
    </div> 

</div> 
"; 

?> 

</div> 
+0

我想,也許有一些明顯的及彼錯過。根據我對你的問題的理解,@ arun-p-johnny的答案是正確的。也許你的問題不清楚? 一個測試讓你嘗試,只是爲了確保事情正常工作:使'.portlet-content'可拖動,並看看會發生什麼。 – user1167442 2013-05-02 02:41:12

+0

當我將它改爲:'$(「#portlet」).draggable(stack:「#portlet」,'它拖動第一個portlet並讓我改變它,但不是第二個。「#set」div是我想讓代碼只移動它,但是它將代碼中的所有div從我的原始代碼中移出。 – derekshull 2013-05-02 02:46:37

+0

這應該是這樣做的: '$('#set')。not($ ('#set div'))。draggable()' – user1167442 2013-05-02 02:52:24

回答

1

看起來要移動的#set不是#set元素的直接孩子。

$(function() { 
    $("#set > div").draggable({ 
     stop: function(event, ui) { 
      var pos_x = ui.offset.left; 
      var pos_y = ui.offset.top; 
      var need = ui.helper.data("need"); 

      console.log(pos_x); 
      console.log(pos_y); 
      console.log(need); 

      //Do the ajax call to the server 
      $.ajax({ 
       type: "POST", 
       url: "updatecoords.php", 
       data: { x: pos_x, y: pos_y, need_id: need} 
      }) 
     } 
    }); 
}); 

演示:Plunker

+0

它停止設置從完全移動...我無法移動螞蟻div然後 – derekshull 2013-05-02 02:31:38

+0

將堆棧:「#set div」,與它有什麼關係? – derekshull 2013-05-02 02:38:45

+0

你可以刪除它並檢查 – 2013-05-02 02:48:06