无法更改Javascript中元素的href
我正在尝试更改Shopify“谢谢”页面上的“继续购物”链接,但它不起作用 我已经在“签出页面设置”的“附加脚本”部分中包含了以下代码无法更改Javascript中元素的href,javascript,html,css,Javascript,Html,Css,我正在尝试更改Shopify“谢谢”页面上的“继续购物”链接,但它不起作用 我已经在“签出页面设置”的“附加脚本”部分中包含了以下代码 <script> (function() { document.getElementsByClassName("step__footer__continue-btn")[0].href = "https://example.com/newlink"; })(); </script> “谢谢”页面上的HT
<script>
(function() {
document.getElementsByClassName("step__footer__continue-btn")[0].href = "https://example.com/newlink";
})();
</script>
“谢谢”页面上的HTML部分如下所示:
<a href="https://example.com/" data-trekkie-id="continue_shopping_button" class="step__footer__continue-btn btn">
<span class="btn__content">Continue shopping</span>
<svg class="icon-svg icon-svg--size-18 btn__spinner icon-svg--spinner-button" aria-hidden="true" focusable="false"> <use xlink:href="#spinner-button" /> </svg>
</a>
它对我有用
<html>
<head>
<script language='javascript'>
function msg(){
var href = document.getElementsByClassName("step__footer__continue-btn");
alert(href[0].href);
document.getElementsByClassName("step__footer__continue-btn")[0].href = "https://example.com/newlink";
alert(href[0].href);
}
</script>
</head>
<body>
<input type="button" value="Click me" onclick="msg()"/>
<a href="https://example.com/" data-trekkie-id="continue_shopping_button" class="step__footer__continue-btn btn">
<span class="btn__content">Continue shopping</span>
<svg class="icon-svg icon-svg--size-18 btn__spinner icon-svg--spinner-button" aria-hidden="true" focusable="false"> <use xlink:href="#spinner-button" /> </svg>
</a>
</body>
</html>
函数msg(){
var href=document.getElementsByClassName(“步骤页脚继续-btn”);
警报(href[0].href);
document.getElementsByClassName(“步骤\uuuu页脚\uuuuu continue-btn”)[0]。href=”https://example.com/newlink";
警报(href[0].href);
}
这很可能是因为JavaScript是在HTML之前加载的。有两种简单的方法可以解决此问题:
1:将您的
标签放在关闭
标签之前,如下所示:
<script>
//Your code
</script>
</body>
请参阅自动加载包装函数中JS部分的可能副本,该函数在DOM就绪时执行。不幸的是,由于某些原因,它在Shopify上不起作用。是的,它在JSFIDLE中对我也起作用,但在Shopify上不起作用,甚至js部分都包装在自动加载函数中,所以DOM必须准备好。谢谢。将包装更改为window.onload=function(){解决了此问题
<script>
//Your code
</script>
</body>
window.onload = function() {
//Your code here
};