javascript - 附加元素时维护 js

标签 javascript jquery

我不是很有经验,希望有人能给我一些提示。

我正在尝试创建一个系统,可以将图像从保存容器传输到可操作容器,在其中可以调整图像大小并拖动图像。

我这里有一个操纵器:Fiddle Demo

我正在尝试让“点击进入/双击退出”系统在这里工作:Fiddle Demo

<div class="frame"></div>

<div class="inventory">
     <images>
</div>


$(".frame img")
    .draggable({
     containment: "parent",
    cursor: "move"
}).resizable({
    aspectRatio:1,
    containment: "parent",
    minHeight: 50,
    minWidth: 50
});


$('.inventory img').click(function(){ 
   $(this).appendTo(".frame");
 });
$('.frame img').dblclick(function(){
   $(this).appendTo(".inventory");
   $(this).removeClass('added');

});

我认为的主要问题是,一旦我追加了 div,js 就不会根据元素的排列刷新和应用。

任何帮助将不胜感激!

谢谢!

最佳答案

由于选择器的评估需要是动态的,因此您需要使用事件委托(delegate)

$('.inventory').on('click', 'img', function () {
    $(this).appendTo(".frame");
});
$('.frame').on('dblclick', 'img', function () {
    $(this).appendTo(".inventory");
    $(this).removeClass('added');
});

演示:Fiddle

注意:这不会处理可调整大小/可拖动的行为 - 当您将元素从一个容器移动到另一个容器时,您需要手动销毁/添加此行为

关于javascript - 附加元素时维护 js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21423117/

相关文章:

javascript - CKEditor 新实例总是卸载

javascript - Jquery 插件只允许文本字段中特定范围内的数字?

javascript - 如何滚动到父 iframe 的顶部按下 iframe 内的按钮?

javascript - jest.fn() 被多次调用

javascript - 如何从其他 html 页面的 html 表单 POST 接收数据?

javascript - Handlebars 和 js : assigning different id values inside for loop and referencing them outside the loop

javascript - 响应字体 : Non-supportive Browsers

javascript - Glyphicons - 不会使用 IE 10 在本地加载; CDN 将加载

php - onchange事件在使用jquery从另一个php页面添加的下拉菜单中不起作用

javascript - 在 ASP.Net 中使用 AJAX PageMethods 从数据库中进行 JQuery AutoComplete TextBox 无法在 Internet Explorer 中工作