jquery – 分页:未捕获TypeError:无法将属性’className’设置为null

前端之家收集整理的这篇文章主要介绍了jquery – 分页:未捕获TypeError:无法将属性’className’设置为null前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试分页显示数据.存在的记录总数是19,我想最初显示3条记录.

这是我的分页代码

// Instantiate pagination after data is available    
pager = new Pager('results',3);
pager.init();
pager.showPageNav('pager','pageNavPosition');
pager.showPage(1);

// pagination object codes.
function Pager(tableName,itemsPerPage) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = 1;
    this.pages = 0;
    this.inited = false;

    this.showRecords = function (from,to) {
        var rows = total_records;
        // i starts from 1 to skip table header row
        for (var i = 1; i < rows.length; i++) {
            if (i < from || i > to) rows[i].style.display = 'none';
            else rows[i].style.display = '';
        }
    }

    this.showPage = function (pageNumber) {



        if (!this.inited) {
            alert("not inited");
            return;
        }

        var oldPageAnchor = document.getElementById('pg' + this.currentPage);
        oldPageAnchor.className = 'pg-normal';

        this.currentPage = pageNumber;
        var newPageAnchor = document.getElementById('pg' + this.currentPage);
        newPageAnchor.className = 'pg-selected';

        var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from,to);
    }

    this.prev = function () {
        if (this.currentPage > 1) this.showPage(this.currentPage - 1);
    }

    this.next = function () {
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1);
        }
    }

    this.init = function () {
        var rows = total_records;
        var records = (rows.length - 1);
        this.pages = Math.ceil(records / itemsPerPage);
        this.inited = true;
    }

    this.showPageNav = function (pagerName,positionId) {
        if (!this.inited) {
            alert("not inited");
            return;
        }
        var element = document.getElementById(positionId);
        var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> &#171 Prev </span> | ';
        for (var page = 1; page <= this.pages; page++)
            pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> | ';
        pagerHtml += '<span onclick="' + pagerName + '.next();" class="pg-normal"> Next &#187;</span>';
        element.innerHTML = pagerHtml;
    }
}

这是我的jsFiddle.

您能告诉我如何解决浏览器控制台中出现的问题吗?

Uncaught TypeError: Cannot set property ‘className’ of null

解决方法

这是你的解决方案,希望这有帮助.. 原文链接:https://www.f2er.com/jquery/241329.html

猜你在找的jQuery相关文章