你好!
我刚从jQuery UI开始,所以我已经被一些东西困住了.
我正在尝试做一个网络程序,它允许用户制作一个平面图。它具有拖放功能。
它有一个菜单,其中有几个可以被拖动的项目。问题是,当它被拖来拖去时,菜单中的项目就消失了(因为它被拖来拖去)。所以我试着用下面的代码来解决这个问题:
jQuery:
$('.notActive').draggable({ //.notActive is an item in the menu
stop: function(){
$(this).removeClass('notActive');
$(this).addClass('active'); //make it an active item
$('#menu').append($('<div class="item notActive">Furniture</div>'));
//this creates a new item in the menu
}
});
每个item
都有类.notActive
,因为它没有被拖动。当用户拖动它时,它会移除该类并接收类.active
。因此,当它被拖动时,它会在#menu
中创建一个带有类.notActive
的新元素。问题是,新创建的(附加(?))菜单中的项不是.draggable()
,即使它有类.notActive
。
为什么会这样呢?如何创建一个新实例,即.draggable()
为语法错误道歉,我是荷兰人。
编辑
这里是Fiddle:https://jsfiddle.net/8tpy7x3e/
发布于 2016-02-06 02:51:16
在添加到菜单中的新项上,您没有显式地调用draggable
。
像这样的东西会有用的:
$('.notActive').draggable({ //.notActive is an item in the menu
stop: function(){
$(this).removeClass('notActive');
$(this).addClass('active'); //make it an active item
//Create a new object
var newItem = $('<div class="item notActive">Furniture</div>');
//Make the new item "draggable" and add it to the menu
newItem.draggable();
$('#menu').append(newItem);
//this creates a new item in the menu
}
});
https://stackoverflow.com/questions/35239977
复制相似问题