我创建了带有很多 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/