使用Jquery根据可放置对象的ID动态更改图像的src

时间:2013-03-18 12:01:23

标签: javascript jquery html image drag-and-drop

我对如何使用Jquery根据可放置对象的ID更改部分图像src感到困惑。

我尝试了这个以及网络上的一些其他想法,遗憾的是这些想法都没有用。如果我必须这样说,这非常棘手。

ui.draggable['<img src= "images/kitchen/.img src" width="800" height="600"/>'].id

目前我只能拍摄一次图像。

我想我应该做一些函数来执行img src。

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Droppable - Revert draggable position</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<style>
#draggable, #draggable1 { width: 100px; height: 100px; padding: 0; float: left; margin: 10px 10px 10px 0; }
#droppable { width: 800px; height: 600px; padding: 0; float: left; margin: 10px; }
</style>
<script>
$(function() {
$( "#draggable" ).draggable({ revert: "valid" });
$( "#draggable1" ).draggable({ revert: "valid" });
$( "#droppable" ).droppable({
activeClass: "ui-state-hover",
hoverClass: "ui-state-active",
drop: function( event, ui ) {
$( this )
    .addClass( "ui-state-highlight" )
    .find( "div" )
    .html('<img src= "http://blakeloizides.co.za/sm/images/preview-images/kitchen/medium-cream-kitchen-preview.jpg" width="800" height="600"/>' + "Dropped! " + ui.draggable[0].id);

}
});
});
</script>
</head>
<body>
<div id="draggable" class="ui-widget-content">
<img src="http://blakeloizides.co.za/sm/images/gallery/img12.png" width="100" height="100">
</div>

<div id="draggable1" class="ui-widget-content">
<img src="http://blakeloizides.co.za/sm/images/gallery/img1.png" width="100" height="100">
</div>

<div id="droppable" class="ui-widget-header">
<div><img src="images/kitchen/kitchen-preview-cad.jpg" width="800" height="600"></div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

这应该有效

            <!doctype html>
            <html lang="en">
            <head>
            <meta charset="utf-8" />
            <title>jQuery UI Droppable - Revert draggable position</title>
            <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
            <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
            <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
            <style>
            #draggable, #draggable1 { width: 100px; height: 100px; padding: 0; float: left; margin: 10px 10px 10px 0; }
            #droppable { width: 800px; height: 600px; padding: 0; float: left; margin: 10px; }
            </style>
            <script>
            $(function() {
            $( "#draggable" ).draggable({ revert: "valid" });
            $( "#draggable1" ).draggable({ revert: "valid" });
            $( "#droppable" ).droppable({
            activeClass: "ui-state-hover",
            hoverClass: "ui-state-active",
            drop: function( event, ui ) {
            var dragElemId = ui.draggable[0].id;
            var imgPath = $('#' + dragElemId).attr("des-image");
            $( this )
                .find( ".drop-image" ).removeAttr("src").attr("src",imgPath);
            }
            });
            });
            </script>
            </head>
            <body>
            <div id="draggable" class="ui-widget-content" des-image="http://blakeloizides.co.za/sm/images/preview-images/kitchen/medium-cream-kitchen-preview.jpg">
            <img src="http://blakeloizides.co.za/sm/images/gallery/img12.png"  width="100" height="100">
            </div>

            <div id="draggable1" class="ui-widget-content" des-image="http://blakeloizides.co.za/sm/images/gallery/img12.png">
            <img src="http://blakeloizides.co.za/sm/images/gallery/img1.png"  width="100" height="100">
            </div>

            <div id="droppable" class="ui-widget-header">
            <div><img class="drop-image" src="images/kitchen/kitchen-preview-cad.jpg" width="800" height="600"></div>
            </div>
            </body>
            </html>