0

我正在对可拖动的 Jquery UI 进行一些实验,但目前我的应用程序无法正常工作,我不明白为什么。这是代码:

<html>
<head>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>

<script type="text/javascript">

$(".productItem").draggable({
    helper: 'clone',
    handle: "productItem"
});

$("#basket").droppable({
    accept: ".productItem",
    drop: function(event, ui){
        $("<div></div>")
            .html(ui.draggable.text())
            .appendTo($(this));
    }
});


</script>
</head>

<body>

<h2>Products</h2>
<div id="list">
<div class="productItem">product 1</div>
<div class="productItem">product 2</div>
<div class="productItem">product 3</div>
</div>

<hr/>

<h2>Basket</h2>
<div id="basket" style = "height: 100px; border: 1px solid red;">

</div>

</body>
</html>

这段代码在 JsFiddle 中工作,但我似乎无法让它在 chrome 中工作,所以我认为这是因为 jQuery 包含。有人可以帮助我吗?

4

2 回答 2

3

将您的 jQuery 代码包装在文档就绪块中:

<script type="text/javascript">
$(document).ready(function() {
    $(".productItem").draggable({
        helper: 'clone',
        handle: "productItem"
    });
    $("#basket").droppable({
        accept: ".productItem",
        drop: function(event, ui) {
            $("<div></div>").html(ui.draggable.text()).appendTo($(this));
        }
    });
});​
</script>

按照上面的方式,代码将在相关元素加载之前尝试并执行。

于 2012-09-17T20:16:37.707 回答
0

找到了答案,问题是我使用的是 jQuery 1.8.0,它不支持 jQuery UI,我改为 jQuery 1.7.2,现在它运行良好。

于 2012-09-18T08:40:25.203 回答