【发布时间】:2015-06-18 22:19:12
【问题描述】:
这是帖子的后续:
JQuery Drag and Drop Count Content of Drop Area?
我对编程很陌生,所以对一个无聊的问题表示歉意。 我正在尝试计算放置在可放置对象中的可拖动元素的数量。与其将计数变量初始化为 0 并在每次可拖动的可拖放区域中下降时增加/减少计数,我正在尝试(可能)不太麻烦的策略,即尝试计算可放置的药丸总数。我试过“.count()”、“.length”但没有成功......这是代码。有什么建议吗?
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<meta charset="utf-8">
<script type="text/javascript" src="js/jquery-1.11.2.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/jquery.ui.touch-punch.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<script src="bootstrap/js/bootstrap.min.js"></script>
<style>
.fish {
margin:.5em;
font-size:1.2em;
position:relative;
display:inline-block;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box;
border-radius: 50%;
width: 40px;
height: 40px;
background: #BAB6B2;
float: left;
border:.3em solid #4B027C ;
}
.cloth {
width: 100%;
height: 210px;
border-radius: 15px;
border: 5% solid rgba(200,0,0,.5);
background-color:white;
background-image: linear-gradient(90deg, rgba(200,0,0,.5) 50%, transparent 50%),
linear-gradient(rgba(200,0,0,.5) 50%, transparent 50%);
background-size:20px 20px;
}
</style>
<script type="text/javascript">
$(init);
function init() {
var j = 0;
$("#counter").find("h1").html("You keep: " + j);
$(".fish").draggable({
addClasses: true,
});
$(".cloth").droppable({
accept: ".fish",
tolerance: "fit",
drop: function( event, ui ) {
j = $(".cloth .fish").length;
$("#counter").find("h1").html("You keep: " + j);
},
out: function(){
j = $(" .cloth .fish ").length;
$("#counter").find("h1").html("You keep: " + j);
}
});
}
</script>
</head>
<body>
<div class="container">
<div id= "counter"><h1>You keep: 0</h1></div>
<div class="cloth" ></div>
<div class = "row ">
<div class="fish" ></div>
<div class="fish" ></div>
<div class="fish" ></div>
<div class="fish" ></div>
<div class="fish" ></div>
</div>
<div class ="row ">
<div class="fish" ></div>
<div class="fish" ></div>
<div class="fish" ></div>
<div class="fish" ></div>
<div class="fish" ></div>
</div>
</div>
<button type="button" class="btn btn-primary footpage" >Send!</button>
</body>
</html>
【问题讨论】:
标签: javascript jquery html jquery-ui