[英]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.