【问题标题】:Wordpress JavaScript TypeError: Cannot read property 'text' of nullWordpress JavaScript TypeError:无法读取 null 的属性“文本”
【发布时间】:2018-02-08 13:31:04
【问题描述】:

我正在使用 wordpress 主题,我试图在主题自定义 JS 中使用以下代码隐藏/替换句子“One Cikanlari”,还尝试了一个名为自定义 CSS 和 JS 的插件:

网址:http://rahafinsaat.com/?lang=tr

 $('h6.snipe.status span').text(function(_,txt) {
return txt.replace('ÖNE ÇIKANLARI', ' ');
});

上述当放入主题自定义JS时,浏览器控制台出现如下错误:

    ?lang=tr:127 Uncaught TypeError: Cannot read property 'text' of null
    at ?lang=tr:127
    at dispatch (jquery.js?ver=1.12.4:3)
    at r.handle (jquery.js?ver=1.12.4:3)
(anonymous) @ ?lang=tr:127
dispatch @ jquery.js?ver=1.12.4:3
r.handle @ jquery.js?ver=1.12.4:3

在自定义 CSS 和 JS 插件中也试过这个:

jQuery('h6.snipe.status span').text(function(_,txt) {
return txt.replace('ÖNE ÇIKANLARI', '');
});

上面没有给出错误,但“ÖNE ÇIKANLARI”这个词仍然显示。

【问题讨论】:

标签: javascript jquery wordpress


【解决方案1】:
  • 首先,您的选择器错误。
  • 第二件事,您的网站中加载了多个 jQuery。
  • 最后,您正在尝试替换大写的文本,但实际上它是小写的。

试试这个...

var jq1124 = jQuery.noConflict();

jq1124(document).ready(function() {
    jq1124('h6.snipe span').text(function(_, txt) {
        return txt.replace('öne çikanlari', '');
    });
})

或者使用纯javascript...

var titleEle = document.querySelector("h6.snipe span");
titleEle.textContent = titleEle.textContent.replace('öne çikanlari', '');

【讨论】:

  • @Ridah,在答案中添加了第二个解决方案。
【解决方案2】:

页面上不存在元素h6.snipe.status span。

试试h6.snipe span

也可能在 DOM 准备好之前触发该功能。需要在 DOM 上的元素准备好后触发该函数。

jQuery(document).ready(function() {
  jQuery('h6.snipe span').text(function(_,txt) {
    return txt.replace('ÖNE ÇIKANLARI', '');
  });
})

【讨论】:

  • 同样的结果,没有错误,但语句仍然显示,您可以查看页面源代码,看看函数在哪里。
猜你喜欢
  • 2014-12-20
  • 1970-01-01
  • 2020-01-29
  • 2022-01-12
  • 2021-12-04
  • 2021-12-17
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多