jquery-ui-rails draggable和droppable函数不起作用

时间:2022-12-06 15:35:23

I am using jquery-ui-rails gem. I want to implement a simple example of draggable and droppable. This is the javascript file:

我正在使用jquery-ui-rails gem。我想实现一个简单的draggable和droppable示例。这是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. 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.

但是,丢弃图像时,droppable p标签的内容不会改变。当我尝试小提琴时,相同的代码工作。我虽然可能是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

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

/* Developer JS */
//= require welcome

Application.css

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

I have also precompiled the assets

我也预先编译了资产

1 个解决方案

#1


2  

it seems the problem is in the 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).

似乎问题出在CSS中。显然,可拖动的li元素比droppable 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:

请看一下这个例子。这表明如果你没有为droppable 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。

#1


2  

it seems the problem is in the 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).

似乎问题出在CSS中。显然,可拖动的li元素比droppable 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:

请看一下这个例子。这表明如果你没有为droppable 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。