【问题标题】:Get id of div where i drag an element获取我拖动元素的 div 的 id
【发布时间】:2011-10-13 05:09:17
【问题描述】:

我有 2 个或更多 div 可放置,并且有一些产品可以拖放到这 2 个或更多 div 中。 在其中一个 div 中放置一个元素后,我想将该元素移动到其他 div 中,并且想知道那个 div id 是什么。

我在谷歌上搜索,我发现了这个: - $(event.target).attr("id") - 这个给我我移动的元素的 id - $(this).parent().attr('id') - 这个给了我元素所在的 div 容器的 ID

移动元素后,我想知道放置元素的 div 的 id。

【问题讨论】:

  • 你在使用 jquery ui 拖放吗?请出示您的代码。
  • 是的。我的脚本是这样的: $( ".existaaa" ).draggable({ snap: true, snapMode: "outer", parent : ".continut" }, { stop: function(event, ui) { } }};
  • 你能发布你的 javascript 来移动元素吗?
  • 是的。该脚本我将它用于已保存到数据库的元素。当我重新加载页面时,我在一个 div 中有一些元素,在另一个 div 中有一些元素。如果我将一个元素从一个 div 移动到另一个,我想知道我放置元素的 id 的 id(新父级 :))。
  • $(this).parent().attr('id') 移动元素后试试这个

标签: jquery drag-and-drop


【解决方案1】:

您可以在drop 回调函数中访问draggabledroppableid - 请参阅this demo

接受@jensgram 的建议 (Utilizing the awesome power of jQuery to access properties of an element) 以提高 this.id 的速度超过 .attr('id')

【讨论】:

    【解决方案2】:

    试试这个——

    $('#droppable').droppable({
       drop: function() {
         $(this).attr('id');
       }
    });
    

    看看这篇文章-some info about drag and drop

    【讨论】:

      【解决方案3】:
      $('#droppable').droppable({
         drop: function() {
       $(this).attr('id');
         }
      });
      

      【讨论】:

      • 为什么不使用this.id?不要为了它而创建一个 jq 对象!
      • 感谢您的回答,但该 sintax 为我提供了我移动的元素的 id。我需要移动元素的 div 的 id
      • @redsquare 确实!以后你可以直接引用Utilizing the awesome power of jQuery to access properties of an element(如果你还不知道的话)。
      • @jensgram 写得不错。如果将 id 传递给 attr,它们应该抛出异常!
      • 代码/语法应该在你删除它之后运行(drop: ...)
      猜你喜欢
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多