Jquery 中的可拖动项目


Draggable Items in Jquery

由于某些原因,即使我启用了div内部的div块也无法拖动。

爪哇语

<script type="text/javascript">
$(document).ready(function() {
$("#boxes_holder").draggable();
});
</script>

网页/PHP

<div id = "boxes_holder" class = "initBox">
<?php
//Creates 49 BOXES elements
    $x = 49;
    for($i=1; $i<=$x; $i++){
       echo '<div id="'.$i.' " draggable="true" class="boxes" style="text-align:center;" > '.$i.'</div>';
        }
?>
</div>

我boxes_holder里面的东西是不可拖拽的,这是为什么?

紧随其后的另一个问题是如何将项目拖到另一个div 框中。

我的另一个div 框:

<div id="dragBox" >
<div id="dragBoxTitle" >
    Pick Your Numbers:
</div>
</div>

我下面的所有代码都按顺序排列:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="en-US" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link rel="stylesheet" href="./css/hw2.css" type="text/css" media="screen" />
<script type="text/javascript"     src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script type="text/javascript">

$(document).ready(function() {
$("#boxes_holder > .boxes").draggable();
});

</script>
</head>
<body>
<div id="mainbody">
<h3>Lotto 6/49 Combinations Finder</h3>

<div id = "boxes_holder" class = "initBox">
<?php
//Creates 49 BOXES elements
    $x = 49;
    for($i=1; $i<=$x; $i++){
       echo '<div id="'.$i.' " draggable="true" class="boxes" style="text-    align:center;" > '.$i.'</div>';
    }
?>
</div>

<div id="dragBox" >
<div id="dragBoxTitle" >
    Pick Your Numbers:
</div>
</div>
</div>
</body>
</html>

我的CSS代码:

body {
padding:0;
margin:0;
font-size: 12px;
font-family: arial, sans-serif;  
color:#000;
cursor:auto;
}
#mainbody {
display:block;
width: 980px;
position: relative;
margin: 40px auto;
}
#mainbody h3{
font-size:24px;
color: blue;
}

#dragBox {
width:400px;
height:250px;
border:0px;
display: inline-block;
float: right;
vertical-align:top;
background-color:#FBFBFB;
}
#dragBoxTitle{
font-weight:bold;
font-size: 150%;
padding: 5px;
background-color: #D0D0D0;
border:1px solid #A9A9A9;
margin: 15px;
vertical-align:top;
}
.boxes {
width: 60px; 
height: 50px;
border:1px solid #A9A9A9;
line-height: 45px;
color: #FF0000;
padding:1px;
margin: 1px;
}

#boxes_holder{
width: 500px; 
height: 500px;
display: inline-block;
}

更新:你缺少jQuery UI库,可拖动功能由jQuery UI提供

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/redmond/jquery-ui.css" media="screen" />
<link rel="stylesheet" href="./css/hw2.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.js"></script>

您正在使boxes_holder元素可拖动,而不是其中的div。

尝试

$(document).ready(function() {
    $("#boxes_holder > .boxes").draggable();
});

演示:小提琴

这对我有用。我怀疑这是你如何包含你的jquery ui

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script>    
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#boxes_holder").draggable();
});
</script></head>
<body>
<div id = "boxes_holder" class = "initBox">
<?php
//Creates 49 BOXES elements
    $x = 49;
    for($i=1; $i<=$x; $i++){
       echo '<div id="'.$i.' " draggable="true" class="boxes" style="text-align:center;" > '.$i.'</div>';
        }
?>
</div>
</body>
</html>