gpt4 book ai didi

javascript - 如何获取不同图像jquery的具体src?

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:46 27 4
gpt4 key购买 nike

我正在开发拖放网络应用程序。我的问题是我可以获得第一个拖动元素的 src。但是当我拖动第二个元素时,它会得到与第一个元素相同的 src。我使用 $(this).find(".drag").attr("src") 获取图像的 src。

例如,

drag the fist element src -> item_head/head45.png

drag the second element src -> item_head/head45.png (but second element src->item_head/head46.png )

<div class="wrapper">
<div id="options">
<?php
$strSQL = "SELECT * FROM item_head ORDER BY ihead_id DESC";
$objQuery = mysqli_query($con,$strSQL);
while($row = mysqli_fetch_array($objQuery)){
?>
<img width="150" height="120" src="item_head/<?php echo $row['filesName'];?>" id="drag1" class="drag"></img>
<?php}?>
</div>

脚本:

$("#frame").droppable({
drop: function(ev, ui) {
if (ui.helper.attr('id').search(/drag[0-9]/) != -1){
counter++;
var element = $(ui.draggable).clone();
element.addClass("tempclass");
$(this).append(element);
$(".tempclass").attr("id","clonediv"+counter);
$("#clonediv"+counter).removeClass("tempclass");
//Get the dynamically item id
draggedNumber = ui.helper.attr('id').search(/drag([0-9])/)
itemDragged = "dragged" + RegExp.$1;
var objsrc = $(this).find(".drag").attr("src");
alert(objsrc);
console.log(itemDragged)
$("#clonediv"+counter).addClass(itemDragged);
var objtop = ui.offset.top - $(this).offset().top;

}
}
});

最佳答案

$(this).find(".drag") 不会返回单个元素。使用 .last()

$("#frame").droppable({
drop: function(ev, ui) {
if (ui.helper.attr('id').search(/drag[0-9]/) != -1){
counter++;
var element = $(ui.draggable).clone();
element.addClass("tempclass");
$(this).append(element);
$(".tempclass").attr("id","clonediv"+counter);
$("#clonediv"+counter).removeClass("tempclass");
//Get the dynamically item id
draggedNumber = ui.helper.attr('id').search(/drag([0-9])/)
itemDragged = "dragged" + RegExp.$1;
var objsrc = $(this).find(".drag").last().attr("src");
alert(objsrc);
console.log(itemDragged)
$("#clonediv"+counter).addClass(itemDragged);
var objtop = ui.offset.top - $(this).offset().top;

}
}
});

关于javascript - 如何获取不同图像jquery的具体src?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28623855/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com