javascript - Jquery 中的可拖动项目

标签 javascript php jquery html jquery-ui

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

Javascript

<script type="text/javascript">
$(document).ready(function() {

$("#boxes_holder").draggable();
});
</script>

HTML/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();
});

演示:Fiddle

关于javascript - Jquery 中的可拖动项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19803254/

相关文章:

javascript - 输入 addEventListener 发生更改时未触发

javascript - 在 Javascript 中生成一个介于 2 个值和 2 个小数位之间的随机数

javascript - Babel 可以在生成的代码中包含 regeneratorRuntime 依赖项吗?

php - 数据库编码问题?显示带问号的双引号和单引号

在 MVC 的 Global.asax 文件中使用 Application_PreRequestHandlerExecute 方法时,Jquery 和 css 在服务器上不起作用

javascript - 使用 SHAKE 效果时消失的元素

javascript - aurelia aurelia-http-客户端 jsonp

php - Symfony 和 Doctrine 元数据缓存

php - 尽可能快地插入多行

javascript - JQuery 设置 CSS Transform, Translate 类中的属性