简体   繁体   English

尝试替换html的Javascript / Jquery未捕获的TypeError

[英]Javascript/Jquery Uncaught typeerror trying to replace html

trying to remove some text on my site using jQuery after the page has finished loading, and the code isn't working. 在页面加载完成后尝试使用jQuery删除我网站上的某些文本,并且代码无法正常工作。 When looking at the JS console in Chrome, I get the following error Uncaught TypeError: Cannot read property 'replace' of undefined . 在Chrome中查看JS控制台时,出现以下错误Uncaught TypeError: Cannot read property 'replace' of undefined

Here's my code for replacing the text: 这是我替换文本的代码:

<script>
window.onload = function(){
  var replaced = $("footer-copy").html().replace("'s Frontpage",'');
  $("footer-copy").html(replaced);
};
</script>

And the HTML I'm trying to update: 我尝试更新的HTML:

<div class="footer-wrap">
    <div class="footer-copy">© 2017 <a href="https://blog.andrewgottschling.me/">Andrew Gottschling's Frontpage</a> Powered by <a href="https://leafpub.org" target="_blank">Leafpub</a></div>
    <div class="footer-design-author"><span>Design with</span> <i class="i-favorite heart"></i> by <a href="https://leafpub.org" target="_blank" title="Desarrollador Web FullStack.">@leafpub</a></div>
</div>

Thanks for all the help in advance. 感谢您提前提供的所有帮助。 :D :D

You need to add a class selector, otherwise jQuery searches for a tag with that name. 您需要添加一个类选择器,否则jQuery将搜索具有该名称的标签。

var replaced = $(".footer-copy").html().replace("'s Frontpage",'');

Notice the dot before the class name. 注意类名前面的点。

要使用类名称查找元素,您需要在该类前面加点号:

$(".footer-copy")

Try to use the class selector "." 尝试使用类选择器“。” before the class name. 班级名称之前。 Like this: 像这样:

<script>
window.onload = function(){
  var replaced = $(".footer-copy").html().replace("'s Frontpage",'');
  $(".footer-copy").html(replaced);
};
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery的/ JavaScript的:未捕获的TypeError? - jquery / javascript: Uncaught TypeError? JavaScript / jQuery“未捕获的TypeError” classList - JavaScript / jQuery “Uncaught TypeError” classList 在HTML5数据属性上使用.replace()并存储用jQuery $ .data()检索到的数字时,出现未捕获的TypeError - Uncaught TypeError when using .replace() on HTML5 data attribute storing numbers retrieved with jQuery $.data() jquery.min.js:2 Uncaught TypeError: Cannot read property 'replace' of undefined (javascript)(laravel) - jquery.min.js:2 Uncaught TypeError: Cannot read property 'replace' of undefined (javascript)(laravel) jQuery.browser:Javascript Uncaught TypeError - jQuery.browser: Javascript Uncaught TypeError Javascript jQuery-未捕获的TypeError:非法调用 - Javascript jquery - Uncaught TypeError: Illegal invocation JavaScript:未捕获的TypeError:jQuery.toJSON不是函数 - JavaScript: Uncaught TypeError: jQuery.toJSON is not a function Jquery / Javascript - 未捕获TypeError:undefined不是函数 - Jquery / Javascript - Uncaught TypeError: undefined is not a function jQuery html()中的JavaScript替换 - JavaScript replace in jQuery html() JavaScript / HTML5 / jQuery拖放上传 - “未捕获的TypeError:无法读取未定义的属性'文件'” - JavaScript/HTML5/jQuery Drag-And-Drop Upload - “Uncaught TypeError: Cannot read property 'files' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM