我試圖建立我的網站,讓圖像被拖動到三個插槽中的一個。拖動工作得很好,但拖放事件不會觸發。我嘗試向drag-stop事件添加警報,以確定它是否被放置在某個區域上,但始終返回false。我調試了JavaScript,無濟於事。這整個設置發生在一個jQuery對話框中,主圖像跨度隱藏,並基於下拉框選擇顯示,並且merch_slots div是動態設置的。任何幫助,將不勝感激。的jQuery拖動圖片到DIV
如果我能得到越來越其圖像拖動到哪個插槽的地步,我可以把剩下的事情。
<span id="span_asst" class="merch_image_slots">
<img id="image_merch_1" class="merchandiser_image" alt="image" src="images/image_merch_1.jpg" />
<img id="image_merch_2" class="merchandiser_image" alt="image" src="images/image_merch_2.jpg" />
<img id="image_merch_3" class="merchandiser_image" alt="image" src="images/image_merch_3.jpg" />
</span>
<div class='merch_slots'>
<div id='selected_item1' class='selected_item'>
<img height='250' class='droppable' src='images/merchandiser/image1.jpg' />
</div>
<div id='selected_item2' class='selected_item'>
<img height='250' class='droppable' src='images/merchandiser/image2.jpg' />
</div>
<div id='selected_item3' class='selected_item'>
<img height='250' class='droppable' src='images/merchandiser/image3.jpg' />
</div>
</div>
$(".merchandiser_image").draggable({
revert: "invalid",
containment: '.container',
refreshPositions: true,
cursor: 'move',
drag: function(event, ui) {
$('.droppable').addClass('ui-state-highlight');
},
stop: function(event, ui) {
$('.droppable').removeClass('ui-state-highlight');
if ($.ui.ddmanager.drop($(this).data("draggable"), event)) {
alert('was dropped');
}
else {
alert('it was not dropped');
}
}
});
$(".selected_item").droppable({
hoverClass: "ui-state-active",
drop: function(event, ui) {
var targetElem = $(this).attr("id");
$(this)
.addClass("ui-state-highlight")
alert(targetElem);
}
});
.selected_item
{
width:122px;
height:250px;
float:left;
background: white;
margin-left:2px;
margin-right:2px;
border-color: #20548E;
border-style: solid;
border-width:2px;
text-align:center;
vertical-align:middle;
}
.merchandiser_image
{
margin-left:2px;
margin-right:2px;
border-color: #20548E;
border-style: solid;
border-width:2px;
height:250px;
}
你的代碼工作得很好,在這裏http://jsfiddle.net/tamilvendhank/VQfWu/1/ – 2011-12-31 04:22:59