我想在点击我的 H2 时重定向到另一个页面。 问题是只有当我的代码中有 http 时它才有效。
<h2 id="btn_share">Share...</h2>
<script type="text/javascript">
document.getElementById("btn_share").onclick = function () {
location.href = "http://www.google.html";
};
</script>
我以谷歌链接为例。我真正想要的是重定向到本地页面,因此我不能使用http://。但这不起作用。而且,如果我只写“www.google.com”,它就不起作用。它仅适用于 http:// 为什么?又该如何解决呢?
(我正在使用 Microsoft Visual Studio)
最佳答案
您应该使用本地路径。
它的工作原理如下:
http://www.google.com
或 //www.google.com
将为您提供 Google 的网站,因为 //
告诉您浏览器使用当前页面的协议(protocol)(例如 http://
)。显然,您可以自己指定。
如果您使用单个 /
为链接添加前缀,它将从当前域的根目录开始。例如,从 http://www.example.com/example/example2.html
链接到 /about
会将用户带到 http:///www.example.com/about
排除 /
或使用 ./
将搜索本地目录。例如,从 http://www.example.com/example/example2.html
链接到 about
会将用户带到 http://www.example.com/example/about
希望这有帮助。
关于javascript - Href 仅适用于 http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33248018/