将ID的路径设为页面路径

时间:2015-04-15 09:39:11

标签: 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

我想让它在我的宝贝地址路径中显示如下:

index.html/div1

怎么可能?

========== UPDATE ==========

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

    $('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提供给我。

3 个答案:

答案 0 :(得分:1)

将它放在.htaccess文件中:

RewriteEngine On

# Specify search friendly URLs
RewriteRule ^index\.html/([A-Za-z0-9-]+)/?$ /index.html#$1 [L]

答案 1 :(得分:1)

在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>

答案 2 :(得分:0)

先生。你可以使用JavaScript吗?它可以在没有服务器的情况下使angularjs $ route服务具有这些功能。 要开始使用,您可以参考https://docs.angularjs.org/tutorial/step_07中为路由托管的教程,并在http://angular.github.io/angular-phonecat/step-7/app/#/phones中进行实时演示。

但是,您会注意到Url中的#符号,我仍然在弄清楚是否可以将其移除以获得您想要的内容。如果论坛中有人可以帮助D先生得到所需要的东西。