如何计算可排序 div 中的元素数量?
How do I count the number of elements in a sortable div?
我在一个 HTML 文档中有多个 div,其中较小的 div 可以相互排序。加载文档时,随机数量的较小 div 会附加到 #boxtop。
这是我的 HTML:
<html>
<body>
<head>
<title></title>
<script link src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="jquery-ui.min.css">
<link rel = "stylesheet" type" type="text/css" href = "style.css">
<script src="jquery-ui.min.js"></script>
<div id = "boxtop" class = "dragInto"></div>
<button type="button" id="button">My Children!!!</button>
<div id = "eqbox"></div>
<div id = "box1" class = "ansBox"></div>
<div id = "box2" class = "ansBox"></div>
<div id = "box3" class = "ansBox"></div>
</head>
</body>
</html>
这是我的相关内容jQuery
$(document).ready(function()
{
$("#boxtop").sortable
({
connectWith: ".ansBox"
});
$(".ansBox").sortable
({
connectWith: ".ansBox"
});
});
$(document).ready(function()
{
$("dragInto").droppable
({
accept: ".box"
});
});
var numChild = $("#boxtop").length;
$(document).ready(function()
{
$("#button").click(function()
{
console.log(numChild);
});
});
我的问题是:如何获得已排序 div 中元素的数量。我目前尝试使用 numChild 将该值打印到控制台,但打印的是“1”。如果我将一堆元素从#boxtop 拖到 .box1,我怎么能得到 .box1 中的元素数量?
.length
属性 告诉您有多少元素属于您调用它的 jQuery object。所以假设 $("#boxtop")
匹配一个元素,$("#boxtop").length
将是 1.
要找出 #boxtop
中有多少元素,您必须 select 它的所有后代,然后 然后 检查 .length
:
// All descendants:
$("#boxtop").find("*").length // or:
$("#boxtop *").length
// Or count immediate children only:
$("#boxtop").children().length
在你的情况下,我认为立即检查 children 可能是你想要的。但是不要设置像这样的全局变量:
var numChild = $("#boxtop").children().length;
...因为这会将 numChild
设置为页面首次打开时的长度,在用户开始与其交互之前。您需要在需要值的地方检查$("#boxtop").children().length
或$(".box1").children().length
。
顺便说一句,您不需要三个单独的 $(document).ready(...)
处理程序:将所有代码合并到一个准备就绪的处理程序中,或者如果您将 <script>
元素放在body 你根本不需要现成的处理程序。
您需要从 sortable
实例捕获 update
或 stop
事件。详细文档就在那里
我在一个 HTML 文档中有多个 div,其中较小的 div 可以相互排序。加载文档时,随机数量的较小 div 会附加到 #boxtop。
这是我的 HTML:
<html>
<body>
<head>
<title></title>
<script link src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="jquery-ui.min.css">
<link rel = "stylesheet" type" type="text/css" href = "style.css">
<script src="jquery-ui.min.js"></script>
<div id = "boxtop" class = "dragInto"></div>
<button type="button" id="button">My Children!!!</button>
<div id = "eqbox"></div>
<div id = "box1" class = "ansBox"></div>
<div id = "box2" class = "ansBox"></div>
<div id = "box3" class = "ansBox"></div>
</head>
</body>
</html>
这是我的相关内容jQuery
$(document).ready(function()
{
$("#boxtop").sortable
({
connectWith: ".ansBox"
});
$(".ansBox").sortable
({
connectWith: ".ansBox"
});
});
$(document).ready(function()
{
$("dragInto").droppable
({
accept: ".box"
});
});
var numChild = $("#boxtop").length;
$(document).ready(function()
{
$("#button").click(function()
{
console.log(numChild);
});
});
我的问题是:如何获得已排序 div 中元素的数量。我目前尝试使用 numChild 将该值打印到控制台,但打印的是“1”。如果我将一堆元素从#boxtop 拖到 .box1,我怎么能得到 .box1 中的元素数量?
.length
属性 告诉您有多少元素属于您调用它的 jQuery object。所以假设 $("#boxtop")
匹配一个元素,$("#boxtop").length
将是 1.
要找出 #boxtop
中有多少元素,您必须 select 它的所有后代,然后 然后 检查 .length
:
// All descendants:
$("#boxtop").find("*").length // or:
$("#boxtop *").length
// Or count immediate children only:
$("#boxtop").children().length
在你的情况下,我认为立即检查 children 可能是你想要的。但是不要设置像这样的全局变量:
var numChild = $("#boxtop").children().length;
...因为这会将 numChild
设置为页面首次打开时的长度,在用户开始与其交互之前。您需要在需要值的地方检查$("#boxtop").children().length
或$(".box1").children().length
。
顺便说一句,您不需要三个单独的 $(document).ready(...)
处理程序:将所有代码合并到一个准备就绪的处理程序中,或者如果您将 <script>
元素放在body 你根本不需要现成的处理程序。
您需要从 sortable
实例捕获 update
或 stop
事件。详细文档就在那里