我正在尝试制作一个可以通过拖放重新定位的元素列表。第一个元素Box 1始终可以正常工作,但有时第二个框也能正常工作,而其他框则无法按预期工作。它们似乎会在您开始拖动它们时一次性触发所有的拖动事件。
如果有影响的话,我正在使用最新版本的Chrome(v23)。
var $questionItems = $('.question-item');
$questionItems
.on('dragstart', startDrag)
.on('dragend', removeDropSpaces)
.on('dragenter', toggleDropStyles)
.on('dragleave', toggleDropStyles);
function startDrag(){
console.log('dragging...');
addDropSpaces();
}
function toggleDropStyles(){
$(this).toggleClass('drag-over');
console.log(this);
}
function addDropSpaces(){
$questionItems.after('<div class="empty-drop-target"></div>');
console.log('drop spaces added');
}
function removeDropSpaces(){
$('.empty-drop-target').remove();
console.log('drop spaces removed');
}
这是 HTML 代码:
<div class="question-item" draggable="true">Box 1: Milk was a bad choice.</div>
<div class="question-item" draggable="true">Box 2: I'm Ron Burgundy?</div>
<div class="question-item" draggable="true">Box 3: You ate the entire wheel of cheese?</div>
<div class="question-item" draggable="true">Box 4: Scotch scotch scotch</div>
这是我的代码的 jsFiddle 链接:http://jsfiddle.net/zGSpP/5/
element.draggable();
初始化元素为可拖动的呢? - jbabey