gpt4 book ai didi

javascript - 删除我的 anchor 中的哈希值

转载 作者:行者123 更新时间:2023-11-29 10:40:29 26 4
gpt4 key购买 nike

我正在使用这段代码在当前 URL 的末尾显示我的循环幻灯片的索引值。

 var index = 0, hash = window.location.hash;
if (hash) {
index = /\d+/.exec(hash)[0];
index = (parseInt(index) || 1) - 1; // slides are zero-based
}

我想删除值前的“#”。我该如何修改这段代码?

最佳答案

所以你正试图从字符串中删除散列:

hash.replace(/#/, '')

var hash = "url.com/#3"
document.getElementById("newUrl").innerHTML = hash.replace(/#/, '');
<div id="newUrl"></div>

@SmokeyPHP 下面指出我使用正则表达式来执行此操作,但您同样可以给 replace 一个字符串参数:hash.replace('#', '')

关于javascript - 删除我的 anchor 中的哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30246097/

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