3

我正在尝试使用fiddle学习 jQuery 拖放。虽然我是 jQuery 新手,但我对 jQuery 和jQuery UI感到很困惑。我还需要安装什么?

我已经安装了jquery-1.6.3.jsjquery-ui.1.8.16.js。但它没有运行。

我怎样才能让它运行?

代码是:

<p class="section">Drag and Drop</p>
<script type="text/javascript" src="/js/jquery-1.6.3.js"></script>
<script type="text/javascript" src="/js/jquery-ui.1.8.16.js"></script>
<script type="text/javascript">
    $('#draggable li').draggable({
        revertDuration: 100,
        helper: function() {
            var selected = $('#draggable img.selected');

            if (selected.length === 0) {
                selected = $('img', $(this)).addClass('selected');
            }
            var container = $('<div class="image-group"/>');
            container.append(selected.clone());
            return container;
        },
        cursorAt: {
            left: 25,
            top: 25
        }
    });

    $('#trash').droppable({
        drop: function(event, ui) {
            var newItems = $(ui.helper).find('img').clone(false).removeClass('selected');
            $(this).append(newItems);
            $('#draggable img.selected').parent().remove();
        }
    });

    $('#draggable li').click(function(event) {
        if (event.ctrlKey) {
            $('img', $(this)).toggleClass('selected');
        }
    });
</script>

<style>
    body {
        font-family:"Trebuchet MS";
    }
    #draggable {
        margin:0;
        padding:10px;
        width:300px;
        list-style-type:none;
        background-color:#000;
    }
    li {
        display:inline;
    }
    img {
        border:5px solid white;
    }
    .image-group img {
        margin-right:5px;
    }
    #trash {
        margin-top:10px;
        width:200px;
        height:200px;
        border:1px dotted #000;
    }
    .selected {
        border-color:#aed0ea
    }
    #trash h4 {
        margin:0;
        padding:0 5px;
    }
    .ui-icon {
        display:inline-block;
    }
</style>

<ul id="draggable">
    <li><img src="http://lorempixel.com/output/nature-q-c-50-50-1.jpg" alt="" /></li>
    <li><img src="http://lorempixel.com/output/nature-q-c-50-50-2.jpg" alt="" /></li>
    <li><img src="http://lorempixel.com/output/nature-q-c-50-50-3.jpg" alt="" /></li>
    <li><img src="http://lorempixel.com/output/nature-q-c-50-50-4.jpg" alt="" /></li>
    <li><img src="http://lorempixel.com/output/nature-q-c-50-50-5.jpg" alt="" /></li>
    <li><img src="http://lorempixel.com/output/nature-q-c-50-50-6.jpg" alt="" /></li>
</ul>
<p>Single left click+drag or Ctrl+left click to select multiple</p>
<div id="trash">
    <h4 class="ui-widget-header">Drop Here<span class="ui-icon ui-icon-trash"></span></h4>
</div>
4

1 回答 1

2

你整个脚本需要被包裹

   $(document).ready(function() {
     // your script

   });

简而言之,

   $(function() {
      // your script

   });
于 2012-05-30T07:30:09.103 回答