gpt4 book ai didi

javascript - JQuery:为什么网站打不开?

转载 作者:行者123 更新时间:2023-12-03 05:33:20 24 4
gpt4 key购买 nike

我对 JQuery 有点陌生,但我每天都在练习。我的目标是在单击按钮后打开链接,但链接似乎没有打开。我正在尝试打开 if 语句内的链接,以便一切都会相应发生。

  window.setInterval(function(){ 
if ($('#add-remove-buttons').find('.button').length > 0) {
$('#size').val($('#size option').filter(function(ind, el) {
return $(el).text() === 'Large';
}).val());
$('#add-remove-buttons').find('.button').trigger('click');
setTimeout(function() {
window.location.replace('http://myweblink');
}, 900);
}
}, 100);

编辑(仍然需要帮助)

我尝试过更改它,但无法加载。我认为它可能会陷入 100 毫秒的循环中。我将函数置于 100ms 循环中,以便它可以检测 if ($('#add-remove-buttons').find('.button').length > 0) 我也刚刚意识到用户单击按钮后,会自动出现以下 html:

<fieldset id="add-remove-buttons"><input class="button remove" name="commit" value="remove" type="submit"><a href="/shop" class="button continue">keep shopping</a></fieldset>

这意味着 if 语句:我的代码中的 if ($('#add-remove-buttons').find('.button').length > 0) 变为 false 并且用于更改 URL 的代码不会运行。有没有办法检测上面的 html 代码是否存在,例如 if 语句变为 false?弄清楚后,我可以输入 window.location.href = "http://myweblink"; 然后让它工作!

最佳答案

并且在您的代码中缺少完整的网址。

使用

 window.location.replace('http://myweblink.com'); 

相反

 window.location.replace('http://myweblink'); 

要重定向,jQuery 不是必需的,window.location.replace(...) 最好模拟 HTTP 重定向。

这比使用 window.location.href = 更好,因为 Replace() 不会将原始页面保留在 session 历史记录中,这意味着用户不会陷入无休止的后退按钮惨败中。如果您想模拟某人点击链接,请使用 location.href。如果您想模拟 HTTP 重定向,请使用 location.replace。

例如:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

您可以阅读答案here .

关于javascript - JQuery:为什么网站打不开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40837491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com