简体   繁体   English

无法使用jQuery .each循环使类中的各个div可拖动

[英]Unable to make individual divs in class draggable using jQuery .each loop

Problem Overview: 问题概述:

I am creating circular div elements to serve as location markers on a map. 我正在创建圆形div元素,以用作地图上的位置标记。 My code reads the total number of rows from a database table and executes a loop to create that number of div elements, assigning each a div id using data returned from the database. 我的代码从数据库表中读取总行数,并执行循环以创建该数量的div元素,并使用从数据库返回的数据为每个div分配一个ID。 Each div element is appended to a single div class (marker_mother). 每个div元素都附加到一个div类(marker_mother)。 All of this works perfectly, resulting in a row of circular div elements on the page. 所有这些都可以完美地工作,从而在页面上产生一行圆形div元素。

The next step is to make each individual div element draggable. 下一步是使每个div元素可拖动。 I am using the .each() jQuery method to loop through all div elements in the class (marker_mother) and set them to draggable using the Draggable interaction from the jQuery UI . 我正在使用.each()jQuery方法来遍历类(marker_mother)中的所有div元素,并使用jQuery UI中Draggable交互将它们设置为可拖动。 I have been using the following Stack Overflow Q&A as a reference: jQuery to loop through elements with the same class . 我一直在使用以下Stack Overflow Q&A作为参考: jQuery遍历具有相同类的元素 However, all my attempts result in the class being set to draggable and not the individual divs. 但是,我的所有尝试都导致该类被设置为可拖动类,而不是各个div。 This means that all divs respond as a unified whole when dragged. 这意味着所有div拖动时都会作为一个整体响应。


Code: 码:

 var total_units = ""; $(document).ready(function() { // Triggers PHP script to return number of table rows from DB $('#get_rows').click(function() { $.get('get_coords.php', function(data) { total_units = data; console.log(data); }); }); // Posts row number to DB and returns query data (eg. id and colour) // Uses returned data in construction of circular div elements $('#create_divs').click(function() { for (i = 0; i < total_units; i++) { $.ajax({ type: 'POST', url: 'get_row.php', dataType: 'html', data: { row: i }, success: function(response) { var jsonData = JSON.parse(response); jQuery('<div/>', { id: jsonData.id, css: { "position": "relative", "top": "200", "left": "100", "border-radius": "50%", "width": "100px", "height": "100px", "background": "jsonData.colour", "font-size": "20px", "text-align": "center", "line-height": "100px", "cursor": "move", "z-index": "100" }, href: 'http://127.0.0.1/' + jsonData.id + '.html', text: jsonData.id }).appendTo('.marker_mother'); console.log(response); } }); } }); // Assigns top&left positions of dragged div to variables // Code to store coords in db will be added later var coordinates = function(element) { element = $(element); var top = element.position().top; var left = element.position().left; } // Loops through divs and makes each draggable $('.marker_mother').each(function(index, item) { $(item).draggable({ start: function() { coordinates(item); }, stop: function() { coordinates(item); } }); }); }); 
 /* CSS to define characteristics for the marker div class */ .marker_mother { position: relative; top: 0; left: 0; border-radius: 50%; width: 50px; height: 50px; font-size: 10px; text-align: center; color: black; line-height: 50px; cursor: move; z-index: 100; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script> 

As mentioned in the overview, I have tried multiple implementations of the .each() function to make the divs draggable (including referring to the DOM object and $(this) ). 如概述中所述,我尝试了.each()函数的多种实现,以使div可拖动(包括引用DOM对象和$(this) )。 All attempts result in the class ( marker_mother ) being set to draggable and not the individual divs. 所有尝试都会导致将类( marker_mother )设置为可拖动而不是单独的div。 I feel there must be something simple I am missing here. 我觉得这里一定缺少一些简单的东西。

Any ideas or suggestions would be greatly appreciated. 任何想法或建议将不胜感激。

Edit: 编辑:

HTML markup for the created divs end up looking as follows: 创建的div的HTML标记最终看起来如下:

<div class="marker_mother ui-draggable ui-draggable-handle"> == $0
  <div id="0001" href="http://127.0.0.1/0001.html" style="position: relative; border-radius: 50%; width: 100px; height: 100px; background: lime; font-size: 20px; text-align: center; line-height: 100px; cursor: move; z-index: 100;">0001</div>
  <div id="0002" href="http://127.0.0.1/0002.html" style="position: relative; border-radius: 50%; width: 100px; height: 100px; background: lime; font-size: 20px; text-align: center; line-height: 100px; cursor: move; z-index: 100;">0002</div>
  <div id="0003" href="http://127.0.0.1/0003.html" style="position: relative; border-radius: 50%; width: 100px; height: 100px; background: lime; font-size: 20px; text-align: center; line-height: 100px; cursor: move; z-index: 100;">0003</div>
</div>

Solution: 解:

haydenwagner provided the solution in an answer below. haydenwagner在下面的答案中提供了解决方案。

$('.marker_mother div').each(function(index, item) {

To me it looks like you are affecting the marker_mother element instead of its children (the divs that you appended). 对我来说,您似乎正在影响marker_mother元素,而不是其子元素(附加的div)。

Try changing this code: 尝试更改此代码:

$('.marker_mother').each(function(index, item) {

to this: 对此:

$('.marker_mother div').each(function(index, item) {

so that the elements that you are making draggable in the each function are the divs inside the .marker_mother element. 因此您在每个函数中可拖动的元素是.marker_mother元素内的div。

If this works then you can add a '.marker' or '.'marker-draggable' class to these divs so that your selection can be more explicit (with the code above, all divs inside the '.marker_mother' will become draggable). 如果可行,则可以在这些div中添加“ .marker”或“ .'marker-draggable”类,以便您的选择更加明确(使用上面的代码,“。marker_mother”内部的所有div都将变为可拖动状态)。 。 This may not be necessary if you are only appending draggable elements to the '.marker_mother' element. 如果仅将可拖动元素附加到'.marker_mother'元素,则可能没有必要。

The issue is that you looping through all elements with the class marker_mother instead of the children. 问题是您使用marker_mother类而不是子类遍历所有元素。 But in this case you don't need the $.each() loop here. 但是在这种情况下,您不需要在这里使用$.each()循环。

Just tweak your selector and draggable can handle the rest: 只需调整选择器, draggable即可处理其余内容:

// Assigns top&left positions of dragged div to variables
  // Code to store coords in db will be added later

  var coordinates = function(element) {
    var top = element.position().top;
    var left = element.position().left;
  }

  // Loops through divs and makes each draggable


  $('.marker_mother div').draggable({
    start: function() {
      coordinates($(this));
    },
    stop: function() {
      coordinates($(this));
    }
  });

Example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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