【问题标题】:Hashtag keeps appearing when I open Bootstrap Modal?当我打开 Bootstrap Modal 时,标签一直出现?
【发布时间】:2025-12-27 02:55:15
【问题描述】:

我不确定为什么会一直这样。我搜索了如何从 URL 中删除主题标签并找到了很多答案。他们都没有帮助,因为他们只是删除了标签,但我不得不刷新页面。即便如此,这仍然没有帮助。我的问题是当我点击这个锚标签时会出现主题标签:

echo '<a  href="?id='.$row['id'].'" <-- here is what I am trying to add to the URL, but the hashtag appears. id="smallbtn" data-toggle="modal" data-target="#small"  data-modal="small">';

这是我用来尝试解决问题的 javascript,但没有成功(在 &lt;head&gt; 中也被告知要使用它。):

 <script type="text/javascript">
// remove fragment as much as it can go without adding an entry in browser history:
window.location.replace("#");

// slice off the remaining '#' in HTML5:    
if (typeof window.history.replaceState == 'function') {
  history.replaceState({}, '', window.location.href.slice(0, -1));
}
</script>

而且我不知道是不是这个问题,但是我用这个打开了modal,把锚标签href放到了URL中:

    <script type="text/javascript">
$(document).ready(function(){
   $(window.location.hash).modal('show');
   $('a[data-toggle="modal"]').click(function(){
      window.location.hash = $(this).attr('href');
   });
});
</script>

我的目标是尝试将该 href 作为参数放在 URL 中,但到目前为止,我很难让它发挥作用。非常感谢您对此的任何帮助!

【问题讨论】:

  • 你需要href="?id='.$row['id'].'"的id吗?如果不需要,请输入href=javascript:void(0)
  • @MohsinMarui 哦,是的,我确实需要身份证。我在我的 php.ini 中使用 GET 方法。不过谢谢你的回答。

标签: javascript jquery html anchor href


【解决方案1】:

在点击事件回调中,你设置window.location.hash = $(this).attr('href');的那一行,这会导致在你设置的内容,即href属性的值之前加上一个'#'前缀。

【讨论】:

  • 啊,我明白了。那么有什么办法可以防止这种情况发生吗? @samizzy
  • 我不确定,但也许你可以用这个代替window.location.origin+=$(this).attr('href')
  • 这实际上在模态中创建了相同的页面。就像两页合二为一。
  • 哦,这不应该发生,现在 window.location.origin 应该是您想要的网址,哦,我明白了,这样做window.location.href= window.location.origin + $(this).attr('href');
  • 好的。刚刚发生了两件事。起初它做了同样的事情,然后它把我重定向到本地主机仪表板。