用/替换#使用javascript

时间:2013-10-09 16:56:28

标签: javascript jquery ajax

是否可以将“我的网址”中的“#”替换为“/”(domain.com#home to domain.com/home)?如果是这样你会怎么做?

$(document).ready(function () {
   $(".con").load("views/startseite.html", function () {[...] });
   $("nav a").click(function (a) {
      a.preventDefault();
      a = $(this).attr("href").split("/").pop().split(".").shift();
      $(".con").hide().load("views/" + a + ".html", function () {
         $(".con").fadeIn("fast",
            function () {[...] });
      document.title = "Domain.com | " + (a.substr(0, 1).toUpperCase() + a.substr(1));
      location.hash = a;
      return !1
   })
   $(document).on('submit', 'form.ajax', function (e) {
      e.preventDefault();
      var that = $(this),
         url = that.attr('action'),
         type = that.attr('method'),
         data = {};
      that.find('[name]').each(function (index, value) {
         var that = $(this),
            name = that.attr('name'),
            value = that.val();
         data[name] = value
      });
      $.ajax({
         url: url,
         type: type,
         data: data,
         success: function () {
            $(".flipbox").flippy({
               color_target: "",
               duration: "500",
               verso: "Anything!",
               onFinish: function () {
                  $("#no-color").css("background-color", "transparent");
               }
            });
         }
      });
   });
   $(document).on('submit', 'form.lebenslauf', function (e) {
      e.preventDefault();
      var $this = $(this);
      $.ajax({
         url: './secure/secure.php',
         type: 'post',
         data: {
            code: $('input[name=bewerbung]').val()
         },
         success: function (resp) {
            if (resp.substr(0, 5) == "Error") {
               $('.error').html(resp);
            } else {
               $('.flipbox').flippy({
                  duration: "500",
                  verso: resp,
                  onFinish: function () {
                     $(".flipbox").css("background-color", "transparent");
                  }
               });
            }
         }
      });
   });
});

示例Html:

<ul>
    <li><a href="/views/home.html">Home</a></li>
    <li><a href="/views/about.html">About</a></li>
    <li><a href="/views/blog.html">Blog</a></li>
    <li><a href="/views/contanct.html">Contact</a></li>
</ul>

我的另一个问题是:当我想使用'if(location.hash)'时,我需要写什么?你会把它放在哪里?为了能够在没有获得主站点的情况下使用domain.com/about等网址。

1 个答案:

答案 0 :(得分:1)

我不确定我们是否在同一页上,请告诉我它是否适合您:

var path = window.location.href;
var newpath = path.replace(/[#]/g, "/");
window.location.href =newpath;