js 文章分页的二种方法

本文介绍了使用JavaScript进行文章分页的两种方法:一种是根据内容的高度进行分页,另一种是通过p标签每30段进行一次分页。详细展示了如何利用jQuery选择器、循环和DOM操作实现分页功能,并提供了相应的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过高度进行分页

html

<div id="showContent">{$article.content|htmlspecialchars_decode}</div>

<div id="articlePages"></div>

css

#showContent {

color:black;

font-size: 16px;

height: 700px;

overflow: hidden;

}

#articlePages {

text-align: right;

}

js

<script type="text/javascript">

var obj = document.getElementById("showContent");

var pages= document.getElementById("articlePages");

//alert(obj.scrollHeight);

window.onload= function()

{

var all=Math.ceil(parseInt(obj.scrollHeight)/ parseInt(obj.offsetHeight));

//获取总页数,主要是应用scrollHeight

pages.innerHTML="共"+ all +"页";

for(var i=1; i<=all;i++)

{

pages.innerHTML +=" <a href=\javascript:showPage('"+i+"');> "+i+"</a> ";

//输出所有页码

}

}

function showPage(pageIndex)

{

obj.scrollTop = (pageIndex-1)* parseInt(obj.offsetHeight);

}

</script>

转载自https://www.cnblogs.com/good10000/archive/2015/08/27/4763787.html

 

通过p标签进行分页(每三十段分一次)

html

<div id="showContent" style="display: none;">{$article.body|html_entity_decode|raw}</div>
<div id="mains"></div>
<div id="articlePages"></div>

js

$(document).ready(function() {
            var l = $('#showContent').children('p').length;
            var pages= document.getElementById("articlePages");
            console.log(l);
            window.οnlοad= function()
            {
                for (var i =0; i <=29; i++) {
                    var main = $('#showContent').children('p').eq(i).html();
                    main = '<p>'+main+'</p>';
                    if (main !== '<p>undefined</p>') {
                        $('#mains').append(main);
                    }
                }
                var all=Math.ceil(l/ 30);
                //获取总页数,主要是应用scrollHeight
                pages.innerHTML="共"+ all +"页";
                for(var i=1; i<=all;i++)
                {
                pages.innerHTML +=" <a href=\javascript:showPage('"+i+"');> "+i+"</a> ";
                //输出所有页码
                }
            }
        });
        function showPage(pageIndex){
            $('#mains').empty();
            for (var i = (pageIndex-1)*30; i <= pageIndex*30 -1; i++) {
                var main = $('#showContent').children('p').eq(i).html();
                main = '<p>'+main+'</p>';
                if (main !== '<p>undefined</p>') {
                    $('#mains').append(main);
                }
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值