2017-01-03 84 views
0

自动添加新的号码页面

$('button').click(function(){ 
 
    $('.content').append('<div class="Box"></div>') 
 
});
.wrap 
 
{ 
 
    position:relative; 
 
} 
 

 
.content 
 
{ 
 
    width:200px; 
 
    height:330px; 
 
    border:1px solid #000; 
 
    position:relative; 
 
    
 
} 
 
.Box 
 
{ 
 
    width:180px; 
 
    height:50px; 
 
    background-color:grey; 
 
    position:relative; 
 
    left:50%; 
 
    margin-left:-90px; 
 
    margin-top:5px; 
 
    top:5px; 
 
} 
 
.numberPage 
 
{ 
 
    width:20px; 
 
    height:20px; 
 
    border:1px solid #666; 
 
    position:relative; 
 
    top:-30px; 
 
    left:170px; 
 
} 
 
.numberPage span 
 
{ 
 
    margin-left:5px; 
 
    margin-top:3px; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="wrap"> 
 
    <div class="content"> 
 
    <div class="Box"> 
 
    </div> 
 
    </div> 
 
<div class="numberPage"> 
 
    <span>1</span> 
 
</div> 
 
</div> 
 

 
<button>Add new box</button>

你好,

我对JavaScript的,我认为一个问题。如何在<中添加5个框后自动添加新号码页。像1 2 3 ... 10。我认为它应该在第五个灰色框之后创建新的当前页码,但是我不知道我该怎么做。 我会很感激的任何建议

+2

你说的是分页? – ricky

回答

1

$('button').click(function(){ 
 
//chek if content height is biger than the sum of box heiht inclding the nmpage height and magin 
 
    if($('.content').height()>(($('.Box').height()+10)*($('.Box').length+1))){ 
 
    $('.content').append('<div class="Box"></div>'); 
 
    }else{ 
 
    var currentPage=parseInt($('.numberPage .active').html()); 
 
    PagesFiled[currentPage]=true; 
 
    currentPage++; 
 
    $('.numberPage .active').removeClass('active'); 
 
$('.numberPage').append('<span class="active">'+currentPage+'</span>'); 
 
    //clear boxes and add in next page 
 
    $('.content').empty(); 
 
    $('.content').append('<div class="Box"></div>'); 
 
    } 
 
}); 
 

 
var PagesFiled={};
.wrap 
 
{ 
 
    position:relative; 
 
} 
 

 
.content 
 
{ 
 
    width:200px; 
 
    height:330px; 
 
    border:1px solid #000; 
 
    position:relative; 
 
    
 
} 
 
.Box 
 
{ 
 
    width:180px; 
 
    height:50px; 
 
    background-color:grey; 
 
    position:relative; 
 
    left:50%; 
 
    margin-left:-90px; 
 
    margin-top:5px; 
 
    top:5px; 
 
} 
 
.numberPage 
 
{ 
 
    display:flex; 
 
    width:20px; 
 
    height:20px; 
 
    
 
    position:relative; 
 
    top:-30px; 
 
    right: -7px; 
 
} 
 
.numberPage span 
 
{ 
 
    margin-left:5px; 
 
    margin-top:3px; 
 
    } 
 
.numberPage .active{ 
 
    border: 1px solid; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="wrap"> 
 
    <div class="content"> 
 
    <div class="Box"> 
 
    </div> 
 
    </div> 
 
<div class="numberPage"> 
 
    <span class='active'>1</span> 
 
</div> 
 
</div> 
 

 
<button>Add new box</button>

+0

谢谢你的回答,这对于我的初学js冒险来说是一个很大的进步,但是我在寻找解决方案,当数字页面将像1,2,3 ... 10一样内嵌时,我将回到上一页 –

+0

已编辑代码推送'PagesFiled'对象中的提交页面; 将点击事件添加到$('。Box'),并返回上一页,如果提交。希望它能帮助你 – nivendha

+0

是的,非常感谢你的帮助,我会尽力去做:) –

0

您可以使用下面的代码为这个

$('button').click(function(){ 
    $('.content').append('<div class="Box"></div>') 
    $('.numberPage').append('<span>'+($('.numberPage span').length + 1)+'</span>') 
}); 
2

试试这个。

$('button').click(function(){ 
 
    if ($('.content .Box').length == 5) { 
 
     $('.content .Box').remove(); 
 
     var number = parseInt($('.numberPage span').text()) + 1; 
 
     $('.numberPage span').html(number); 
 
    } 
 
    $('.content').append('<div class="Box"></div>') 
 
});
.wrap 
 
{ 
 
    position:relative; 
 
} 
 

 
.content 
 
{ 
 
    width:200px; 
 
    height:330px; 
 
    border:1px solid #000; 
 
    position:relative; 
 
    
 
} 
 
.Box 
 
{ 
 
    width:180px; 
 
    height:50px; 
 
    background-color:grey; 
 
    position:relative; 
 
    left:50%; 
 
    margin-left:-90px; 
 
    margin-top:5px; 
 
    top:5px; 
 
} 
 
.numberPage 
 
{ 
 
    width:20px; 
 
    height:20px; 
 
    border:1px solid #666; 
 
    position:relative; 
 
    top:-30px; 
 
    left:170px; 
 
} 
 
.numberPage span 
 
{ 
 
    margin-left:5px; 
 
    margin-top:3px; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="wrap"> 
 
    <div class="content"> 
 
    <div class="Box"> 
 
    </div> 
 
    </div> 
 
<div class="numberPage"> 
 
    <span>1</span> 
 
</div> 
 
</div> 
 

 
<button>Add new box</button>

+0

谢谢,它对我来说是一大步,但我想在内联numberPage,因为如果我将返回到上一页?我有可能在前端做这件事,或者这件事需要做在后端? –

+0

这可以用JS来完成,但是你必须使用之前附加的子元素来制作内容框,所以当你转到前一页时,你可以看到它们。一旦页面被加载,它将会丢失。如果你想让你不得不保存当前状态。 – aavrug

+0

好的,谢谢,我会尽力而为 –

0

尝试这种解决方案

t = 0; 
 
$('button').click(function(){ 
 
    $('.content').append('<div class="Box"></div>'); 
 
    t++; 
 
    if (t%5 === 0) { 
 
    $('.wrap').append('<div class="numberPage"><span>'+t+'</span></div>'); 
 
    
 
    } 
 
});
.wrap 
 
{ 
 
    position:relative; 
 
} 
 

 
.content 
 
{ 
 
    width:200px; 
 
    height:330px; 
 
    border:1px solid #000; 
 
    position:relative; 
 
    
 
} 
 
.Box 
 
{ 
 
    width:180px; 
 
    height:50px; 
 
    background-color:grey; 
 
    position:relative; 
 
    left:50%; 
 
    margin-left:-90px; 
 
    margin-top:5px; 
 
    top:5px; 
 
} 
 
.numberPage 
 
{ 
 
    width:20px; 
 
    height:20px; 
 
    border:1px solid #666; 
 
    position:relative; 
 
    top:-30px; 
 
    left:170px; 
 
} 
 
.numberPage span 
 
{ 
 
    margin-left:5px; 
 
    margin-top:3px; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="wrap"> 
 
    <div class="content"> 
 
    <div class="Box"> 
 
    </div> 
 
    </div> 
 
<div class="numberPage"> 
 
    <span>1</span> 
 
</div> 
 
</div> 
 

 
<button>Add new box</button>

1

试试这个。添加当前页面号每点击一次按钮+1。

$('button').click(function(){ 
 
    var counter = $('.numberPage span').text(); 
 
    $('.content').append('<div class="Box"></div>'); 
 
    
 
    if($('.content .Box').length == 6){ 
 
     $('.content .Box').remove(); 
 
     counter = parseInt(counter) + 1; 
 
     $('.numberPage span').text(counter); 
 
    } 
 
    
 
});
.wrap 
 
{ 
 
    position:relative; 
 
} 
 

 
.content 
 
{ 
 
    width:200px; 
 
    height:330px; 
 
    border:1px solid #000; 
 
    position:relative; 
 
    
 
} 
 
.Box 
 
{ 
 
    width:180px; 
 
    height:50px; 
 
    background-color:grey; 
 
    position:relative; 
 
    left:50%; 
 
    margin-left:-90px; 
 
    margin-top:5px; 
 
    top:5px; 
 
} 
 
.numberPage 
 
{ 
 
    width:20px; 
 
    height:20px; 
 
    border:1px solid #666; 
 
    position:relative; 
 
    top:-30px; 
 
    left:170px; 
 
} 
 
.numberPage span 
 
{ 
 
    margin-left:5px; 
 
    margin-top:3px; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="wrap"> 
 
    <div class="content"> 
 
    <div class="Box"> 
 
    </div> 
 
    </div> 
 
<div class="numberPage"> 
 
    <span>1</span> 
 
</div> 
 
</div> 
 

 
<button>Add new box</button>

+0

感谢您的回答,但我不想分页“灰色框”,但内容后添加第五个框 –

+0

谢谢,很好的工作,但可以使页码内联?因为如果我想回到上一页,该怎么办? –