简体   繁体   English

jQuery UI UI Rails的可拖放功能不起作用

[英]jquery-ui-rails draggable and droppable function does not work

I am using jquery-ui-rails gem. 我正在使用jquery-ui-rails gem。 I want to implement a simple example of draggable and droppable. 我想实现一个可拖动和可拖放的简单示例。 This is the javascript file: 这是javascript文件:

$('.draggable_images').draggable();
    $('#droppable1').droppable({
     drop: function(event,ui){
     $(this).addClass("ui-state-highlight").find("p").html("Added!");
  }
});

But the contents of the droppable p tag does not change when an image is dropped. 但是,放置图像时,droppable p标签的内容不会更改。 The same code worked when i tried in fiddle. 当我尝试小提琴时,相同的代码有效。 I though it could be the issue with jquery-rails and the jquery-ui-rails compatibility, but the versions of jquery-rails is 3.1.2, and ui-rails is 5.0.3. 我虽然可能是jquery-rails和jquery-ui-rails兼容性的问题,但是jquery-rails的版本是3.1.2,而ui-rails是5.0.3。

What am I doing wrong? 我究竟做错了什么?

These are the other files: 这些是其他文件:

the html file ` html文件

<% @ingredients.each do |ingredient| %>
  <li class = "draggable_images">
    <%= image_tag Ingredient.where(id: ingredient.ingredient_id).first.image_url%>
  </li>
<% end %>
</ul>

<style>
   #droppable1 { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; border-style: solid;}
</style>

<div id="droppable1">
  <p>Drop here</p>
</div>

Application.js Application.js

//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require turbolinks
//= require_tree .

/* Developer JS */
//= require welcome

Application.css Application.css

*= require jquery-ui
 *= require_tree .
 *= require_self

I have also precompiled the assets 我还预编译了资产

it seems the problem is in the CSS. 看来问题出在CSS中。 Apparently the draggable li element is bigger than the droppable div (because the li is block size and the div has a 150px width). 显然,可拖动的li元素大于可放置的div(因为li是块大小,并且div的宽度为150px)。

Please take a look at this example . 请看这个例子 That shows that if you didn't set float, width and height for the droppable div, like so: 这表明如果您没有为可放置div设置float,width和height,则如下所示:

#droppable1 { padding: 0.5em; margin: 10px; border-style: solid; }

Then your code should work correctly. 然后,您的代码应该可以正常工作。

The other option is to limit the size of the draggable li so it fits the destination div. 另一个选择是限制可拖动li的大小,使其适合目标div。

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

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