gpt4 book ai didi

javascript - 使用 JQuery 按钮更改 URL #ID

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

我正在尝试创建一个函数,在每次单击按钮时,页面 URL 末尾的 ID 都会加 1。所以,

example.org/page -> (buttonclick) -> example.org/page#1 -> (buttonclick) -> example.org/page#2 等等

我现在得到的是:

var anchor = 0;

$('#bottom-right').click(function() {
var anchor += 1;
var anchorurl = "#" + anchor;
var currenturl = window.location.href;
var newurl = currenturl.slice(0, -1) + anchorurl;
window.location.replace(newurl);
});

但它并没有真正起作用&我想有一个更清洁的解决方案...

提前致谢。

最佳答案

简单的做

$('#bottom-right').click(function() {
window.location.hash = ++anchor;
});

利用内置的hash 方法。

关于javascript - 使用 JQuery 按钮更改 URL #ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36663676/

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