javascript - 将 ID 的路径作为页面路径

标签 javascript jquery html css

我创建了带有很多 div 的 html 页面:

<div id="div1">
   <!-- Some code -->
</div>

<div id="div2">
   <!-- Some code -->
</div>

<div id="div3">
   <!-- Some code -->
</div>

有很多<a>标签在哪里 href有这些 <div>s 的 ID ,当我点击链接时,它会显示 <div>在我的浏览器地址路径中,它显示了这个:

index.html#div1

我希望它像这样显示在我的 bowsers 地址路径中:

index.html/div1

这怎么可能?

==========更新==========

我从互联网上搜索并编写了这段代码:

    $('ul.nav li a').click(function(e){

            e.preventDefault();


            var $this = $(this);

            var $href = $this.attr('href');

            window.history.pushState({}, '', $href);

        });

我检查了我点击的值 href它给了我这样的东西:

http://path/index.html#div1

我试图通过替换 # 将其编辑为字符串与 / , 并将其放在 windows.history.pushState 上但现在它给了我SecurityError .

最佳答案

在 web.config 文件中添加以下代码。

<system.webServer>
    <rewrite>
      <rules>   
        <rule patternSyntax="ExactMatch" name="div1">
          <match url="index.html/div1" />
          <action type="Rewrite" url="index.html#div1" />
        </rule>
        <rule patternSyntax="ExactMatch" name="div2">
          <match url="index.html/div2" />
          <action type="Rewrite" url="index.html#div2" />
        </rule>
      </rules>
    </rewrite>
</system.webServer>

关于javascript - 将 ID 的路径作为页面路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29646776/

相关文章:

javascript - 如何在 Three.js 中将场景或模型导出为 GLTF 格式?

javascript - 如何使用 Javascript/jQuery 进行初始大写

jQuery 如果链接 hasClass,则将类应用于其父级

javascript - WinJS Flyout 不断关闭 - 无法单击控件

javascript - 在鼠标悬停时隐藏所有靠近光标的跨度?

javascript - Symfony/Jquery 对象集合

javascript - 如何防止 knockout 嵌套排序允许在子列表中删除?

javascript - 无法选择div的输入

javascript - jQuery 从 UL 层次结构中删除类,除了

javascript - 在 DOM 中找不到 nextElement/nextElementSibling。是因为它是 img 吗?