簡體   English   中英

如何獲取不同圖像jQuery的特定src?

[英]How to get the specific src of different image jquery?

我正在開發拖放式Web應用程序。 我的問題是我可以獲取第一個拖動元素的src。 但是,當我拖動第二個元素時,它將獲得與第一個元素相同的src。 我使用$(this).find(".drag").attr("src")獲得圖像的src。

例如,

拖動拳頭元素src-> item_head / head45.png

拖動第二個元素src-> item_head / head45.png(但第二個元素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;

            }
        }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM