未捕获的TypeError:无法将属性'className'设置为null [英] Uncaught TypeError: Cannot set property 'className' of null

查看:230
本文介绍了未捕获的TypeError:无法将属性'className'设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用js文件对表进行分页。当我尝试单击下一页按钮时发生错误。它只是显示错误,如未捕获的TypeError:无法设置属性'className'为null。

I am using js file for paginate a table. It occurs an error while I am trying to click on next page button. It just show the error like this "Uncaught TypeError: Cannot set property 'className' of null".

这是我的代码:

    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'; 


推荐答案

它失败,因为没有带有id的DOM元素 'PG' + this.currentPage 。如果这是正常行为,那么您可以将 className 调用包装在中,如果块:

It fails because there is no DOM element with the id 'pg'+this.currentPage. If this is normal behavior, then you can just wrap the className call in an if block:

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

否则,您需要发布更多代码以向我们展示 this.currentPage 在JavaScript中设置,以及它正在运行的HTML。

Otherwise, you'll need to post more code to show us where this.currentPage is set in JavaScript, and the HTML on which it is acting.

这篇关于未捕获的TypeError:无法将属性'className'设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆