2017-07-30 62 views
0

試圖刪除我的網站上的一些文本使用jQuery頁面加載完成後,代碼無法正常工作。在Chrome瀏覽器中查看JS控制檯時,出現以下錯誤Uncaught TypeError: Cannot read property 'replace' of undefinedJavascript/Jquery uncaught typeerror試圖替換html

這裏是我的替換文本代碼:

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

我試圖更新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> 

感謝提前所有幫助。 :d

回答

4

您需要添加一類選擇,否則的jQuery搜索該名稱的標籤。

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

注意類名前面的點。

+0

謝謝。不知道我是如何錯過的。 – agottschling

2

要使用的類名找到的元素,你需要在前面加上這個類點:

$(".footer-copy") 
0

嘗試使用類選擇器「。」在課程名稱之前。像這樣:

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