小编Ian*_*Ian的帖子

如何使用jQuery在URL中将斜杠更改为哈希?

我需要用URL中的哈希替换斜杠。其他一切保持不变。
例如

www.example.com/main/events/event-1  
Run Code Online (Sandbox Code Playgroud)

需要更改为:

www.example.com/main/events#event-1  
Run Code Online (Sandbox Code Playgroud)

(jQuery解决方案是最佳选择,插件可以)

根据OP的评论进行更新:

使用此代码:

function outputStatus(e)
{
  if (e.success && $.url.segment(1) == 'events')
  {
    // IF Flash SWF loads success AND on events page
    var url = $.url.attr('source'); // gets current URL
    var new_url = url.replace(/\/([^\/]+)$/, "#$1"); // replaces last slash with a hash
    window.location = new_url; // sets the current URL to the new URL
  }
}
Run Code Online (Sandbox Code Playgroud)

该URL被更改了两次(因此www.example.com/main/events/event-1变为www.example.com/main/events#event-1,然后变为www.example.com/main#events#event-1)。

javascript jquery url-rewriting

2
推荐指数
1
解决办法
1459
查看次数

标签 统计

javascript ×1

jquery ×1

url-rewriting ×1