我有一个不同的场景,我想将点击事件的页面重定向到一个包含 # 符号的链接/页面。当我点击它时,页面保持在同一个位置,不重定向,只是替换 url 框中的 url。我想这是因为 # 符号,当我用另一个没有 # 符号的链接替换时它工作正常。我无法从页面链接中删除它,如何重定向我的页面?我想在 jQuery 中执行此操作
编辑:我不想重新加载我的页面
edit: not duplicate 我的链接在#号前后用斜线“/”分隔,没有附上页面名称
我要重定向的链接
myApp/#/myPage.html
或者只是
#/myPage.html
HTML
<button id="btn">Click</button>
或
<a id="btn" href="#">Click</a>
我试过这些..
<button onClick="window.location='#/myPage.html';">Click</button>
和
$(document).on('click', '#btn', function (e) {
window.location.href = "myApp/#/myPage.html";
//window.location.href = "#/myPage.html"; // this too tried
});
还有这些
window.location.href = "#/myPage.html";
$(location).attr('href','#/myPage.html');
最佳答案
只要 URL 保持不变,浏览器就不会发出新的页面请求。您可以像这样向 URL 添加一些查询参数,缺点是地址栏中的 URL 稍微不那么漂亮:
$(document).on('click', '#btn', function (e) {
window.location.href = "?"+Date.now()+"#/myPage.html";
});
关于javascript - 在 jQuery 中使用按钮或 anchor 重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39117404/