我有3页。每个页面都有同一个类的父div。用户可以从一个页面水平滚动到另一个页面。此外,用户还可以动态地将子div添加到父容器中。用户将开始从父级添加div(在第1页中)。
我有两个问题:
请帮帮我。这一切我都是新手。
提前谢谢。
相关图片:

样本代码::
<script>
$("addbutton").click(function(){
$("#child").appendTo(".parent");
});
</script>
//page 1
<div class= "parent">
<div id= "child1">
</div>
<div id= "child2">
</div>
<div id= "child3">
</div>
</div>
//page 2
<div class= "parent">
</div>
//page 3
<div class= "parent">
</div>发布于 2014-06-06 18:49:46
以下是我想出的方法:
var $pages = $('.page');
var child = '<div class="child"></div>';
$('button').on('click', function () {
var childAdded = false;
$pages.each(function () {
var $page = $(this);
if ($page.children().length < 3) {
$page.append(child);
childAdded = true;
return false;
}
});
if (!childAdded) {
alert('You can not add anymore divs!');
}
});我循环遍历每个.page并检查孩子的数量。如果没有3个子级,我会添加一个,然后返回false从循环中转义。
发布于 2014-06-06 18:50:25
if($(".parent").children().length < 4) {
console.log('Less then or equal to 3')
}else {
console.log('Cannot add any more child elements')
}发布于 2014-06-06 18:48:47
尝尝这个
$("addbutton").click(function(){
if($(".parent").children().length<3)
{
$("#child").appendTo(".parent");
}
else
{
alert("You can not add any more divs");
}
});https://stackoverflow.com/questions/24088343
复制相似问题