gpt4 book ai didi

javascript - location.href 指向没有 http ://的链接

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:59:53 26 4
gpt4 key购买 nike

我有一个网页 - http://www.example.com/ - 我有一个按钮。单击按钮后,我需要转到特定链接 (www.google.com)。按钮就像:

<input type="button"
onclick="javascript:location.href='www.google.com'"
value="Click" />

但是这个按钮会打开一个页面“http://www.example.com/www.google.com”,这是错误的 URL。

我试过window.locationdocument.locationdocument.location.hreflocation.href 但一切都是徒劳的。

我的onclick中的URL不能限制以'http://'开头。

最佳答案

您可以通过在 URL 前加上 // 前缀来使用“当前”协议(protocol)

关于javascript - location.href 指向没有 http ://的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12214206/

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