我一直在尝试使用自定义 JavaScript 更改位于页脚区域的品牌图像(在 WordPress 中)的 a href,我认为图像和 URL 是在此处定义的:
<!-- !Bottom-bar -->
<div id="bottom-bar" role="contentinfo">
<div class="wf-wrap">
<div class="wf-container-bottom">
<div class="wf-table wf-mobile-collapsed">
<div id="branding-bottom" class="wf-td"><a href="https://change this url.com/"><img class=" preload-me" src="default.jpg" srcset="default.jpg 240w, default.jpg 240w" width="240" height="60" sizes="240px" alt="default" /></a></div>
<div class="wf-td">
<div class="wf-float-left">
</div>
</div>
我尝试了一些解决方案,但似乎都无法正常工作。
<script>
document.getElementById("branding-bottom").href = "https://www.newpageurl.html";
</script>
在其他帖子中,此代码已成功用于更改 href 和图像本身,但我只能在尝试加载页面时看到此错误。
Uncaught TypeError: Cannot set property 'href' of null at https://change this url.com/:366
我很感谢我能得到的每一个帮助,这对我来说仍然有些新鲜和困惑。谢谢!
最佳答案
branding-bottom
是 <div>
并且没有href
您想要做的是获取 branding-bottom
的 child
这应该有效:
document.getElementById("branding-bottom").children[0].href = "https://www.newpageurl.html";
document.getElementById("branding-bottom").children[0].href = "https://www.newpageurl.html";
<div id="branding-bottom" class="wf-td">
<a href="https://change this url.com/">link change</a>
</div>
关于javascript - 使用自定义 JavaScript、WordPress 更改页脚区域中图像的 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45209026/