面向对象编程 - JavaScript用对象的概念写一个简单的分页
迷茫
迷茫 2017-04-11 11:57:18
[JavaScript讨论组]

初次接触对象,现在想写一个简单的分页
<body>

<a href="#" id="first">首页</a>
<a href="#" id="pre">上一页</a>
<span id="pageNum">当前页数</span>/<span id="pageCount">总页数</span>
<a href="#" id="next">下一页</a>
<a href="#" id="last">最后页</a>


<script>
    

    var page = (function(){
        var pageNum = 1;
        var pageCount = 10;
        return {
            getPageNum :function(){
                return pageNum;
            },
            getPageCount :function(){
                return pageCount;
            },
            first :function(){
                pageNum = 1;
            },
            last :function(){
                pageNum = 10;
            },
            next:function(){
                pageNum++;
            },
            pre:function(){
                pageNum--;
            }
        }
    })
</script>

</body>
这段js代码是一大牛提供的,但是接下来不知道怎么写,请各位大神帮忙写完整,感谢

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(1)
ringa_lee

<body>

<p id="pagebar">
    <a href="#" v-if="pageNumber>1" v-on:click="firstPage">首页</a>
    <a href="#" v-if="pageNumber>1" v-on:click="prevPage">上一页</a>
    {{pageNumber}}/{{pageCount}}
    <a href="#" v-if="pageNumber<pageCount" v-on:click="nextPage">下一页</a>
    <a href="#" v-if="pageNumber<pageCount" v-on:click="lastPage">最后页</a>
</p>

<script src="js/vue.js"></script>
<script>
    


    //构造方法
    var Page = function (recordCount,pageSize) {
        // body...
        this.recordCount = recordCount;
        this.pageSize = pageSize;
        this.pageNumber = 1;
        this.pageCount = Math.ceil(recordCount/pageSize);
    }//this 指向当前对象

    Page.prototype.firstPage = function() {
        // body...
        this.pageNumber = 1;
    }
    Page.prototype.prevPage = function(){
        if(this.pageNumber>1){
            this.pageNumber--;
        }
    }
    Page.prototype.nextPage = function(){
        if(this.pageNumber<this.pageCount){
            this.pageNumber++;
        }
    }
    Page.prototype.lastPage = function(){
        this.pageNumber = this.pageCount;
    }

    var page = new Page(101,10);
    var pageBar = new Vue({
        el:'#pagebar',
        data:{
            pageNumber:page.pageNumber,
            pageCount:page.pageCount
        },
        methods:{
            firstPage:function(){
                page.firstPage();
                this.pageNumber=page.pageNumber;
            },
            prevPage:function(){
                page.prevPage();
                this.pageNumber=page.pageNumber;
            },
            nextPage:function(){
                page.nextPage();
                this.pageNumber=page.pageNumber;
            },
            lastPage:function(){
                page.lastPage();
                this.pageNumber=page.pageNumber;
            },
        }
    })
</script>

</body>

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号