jquery - 当拖放到不同的 div 上时,如何将可拖动的 div 附加到不同的 div?
问题描述
我正在尝试将菜单中的图标拖放到页面上的 div 上,然后将其附加到它被放置的 div 上,而不是附加到一个单独的 div 上。
例如,我希望能够将图标拖出菜单,将其放在 div“dropover_target”上,以便它可以拾取其类的一部分,但最终将图标附加到 div“actual_target”。
我设想html会像这样开始:
<div id="menu">
<div id="icon" class="none"></div>
</div>
<div id="dropover_target" class="area"></div>
<div id="actual_target"></div>
但最终是这样的:
<div id="menu"></div>
<div id="dropover_target"></div>
<div id="actual_target">
<div id="icon" class="area+stuff"></div>
</div>
我已成功使用以下 jQuery 获取 dropover_target div 的类并将其设置为图标的类。但是我不能将图标 div 附加到 actual_target div。
function dragDrop(ev) {
ev.preventDefault();
var icon = ev.dataTransfer.getData("text");
var area = 'pos-' + ev.target.id; //get the id of the dropover_target
$('#'+icon).addClass(area); //add a new class to the icon
ev.target.appendChild(document.getElementById(token));
//this line appends the icon to the dropover_target - which is not what I want to do!
$('#actual_target').appendChild(document.getElementById(token));
//this line is to append the icon to the actual target - but does not seem to want to work!
}
解决方案
推荐阅读
- android - 第一次更新后未触发 Firebase 数据库 onChildChanged()
- django - Django 与 BeautifulSoup 的 lxml 解析器的兼容性
- javascript - 如何使用codemirror显示像mozilla开发网络一样的代码输出?
- css - CSS @keyframes 动画在悬停时不断闪烁
- encryption - 如何使用 Virgil 安全加密和解密消息?
- google-app-engine - 谷歌应用引擎项目多区域部署
- angular - 我们如何在 Angular 4 应用程序中为特定的 html 页面导入条件 css 文件?
- android - 更改系统语言时,Android 复数无法正常工作
- scala - 如何在 Play 自己的代码中使用 Status
- javascript - javascript中的mouseChildren之类的东西