如何使用默认 ID 检查路径名

How to check the pathname with a default id

我想检查我的路径名以在 Jquery

中突出显示我的当前选项卡

在这种情况下,我有 4 个路径名:

而且我想写一个这样的脚本来执行它。但是目前,我的尝试不起作用。

var pathname = window.location.pathname;
var hash = window.location.hash
if(pathname == '/' || pathname == '/groups/'hash'/' ){
  alert('yo');
} else if(pathname == '/tasks' || pathname == '/groups/'hash'/task_board' ){
  alert('ya');
} else{
  alert('none');
}

注意:这是在页眉中,所以我不知道group_id,所以我需要把它抓到当前页中。你知道一个简单的方法吗?

if(路径名== '/' || 路径名== '/groups/#id/' ){

你需要整理你的条件测试。

您的 if 条件应如下所示:

if(/\/tasks/g.test(pathname) || /\/groups\/.*\/task_board/g.test(pathname) )

这里我用了一个regular expression这样无论你要找什么ID都可以匹配字符串:

模式 /\/groups\/.*\/task_board/g 表示,'/groups/ ANYTHING /task_board/'

你可以尝试类似的方法:

  var pathname = window.location.pathname;
  var group = pathname.split("/")[2]
  if(pathname == '/' || pathname == '/groups/'+ group ){
    alert('yo');
  } else if(pathname == '/tasks' || pathname == '/groups/'+ group +'/task_board' ){
    alert('ya');
  } else{
    alert('none');
  }

请注意,该组会捕获您的 group.id 并将其注入 href