我有一个末尾带有哈希值的网址:test123.com/go#whathaveyou
我如何将哈希值附加到按钮 onclick url 目标中的目标 url:
<form target="_blank" id="form1">
<input onclick="window.location.href='test123.com/newpage'+location.hash;" type="Submit" value="GO" style="border-radius: 5px;"/></form>
最终目标是转到附加了哈希值的新页面:test123.com/newpage#whathaveyou
最佳答案
假设您为该输入提供 id“link”:
var elem = document.getElementById('link');
var url = window.location.href;
var res = url.split("#");
var hash = res[1];
elem.addEventListener('click, function () {
window.location = https://test123.com/newpage'+ hash;
});
您可以大量压缩此代码,只是希望它尽可能具有可读性。
关于javascript - 将当前 URL 中的 location.hash 附加到目标 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54411626/