Javascript 将类添加到regexjquery
我有这个Javascript 将类添加到regexjquery,javascript,jquery,Javascript,Jquery,我有这个 if( window.location.href == "http://localhost:3000/categories") { $("#container ul #all_categories a.categories-menu").css("font-weight","bold"); } }); 但我有一个url: http://localhost:3000/username/boards 用户名是动态用户名,例如 http://localhost:3
if( window.location.href == "http://localhost:3000/categories") {
$("#container ul #all_categories a.categories-menu").css("font-weight","bold");
}
});
但我有一个url:
http://localhost:3000/username/boards
用户名是动态用户名,例如
http://localhost:3000/michael-21/boards
http://localhost:3000/Richard_10/boards
http://localhost:3000/Mary.50/boards
并为每个用户进行更改
如何使用最后一个url实现相同的功能
谢谢您不需要正则表达式。以下方法更简单,也更易于维护(将页面移动到生产环境时,不必更改代码中的URL): 前一个也将匹配
/me/not/boards
。如果您不想这样做:
var index = location.pathname.indexOf('/boards', 1);
index !== -1 && location.pathname.lastIndexOf('/', index-1) === 0
最后,正则表达式方法:
/^\/[^/]+\/boards/.test(location.pathname)
实现示例
您的选择器不需要多个ID,这将起作用,而且可能会更快(甚至更快):
$(“#all_categories.categories menu”)
。您可以为此url编写一个示例吗http://localhost:3000/michael-灰色/板
带条件和添加类?谢谢你,Rob wwou@RobW工作得很好。非常感谢你!。请原谅我迟迟没有回答。再次感谢:)。如果在.com域之后没有任何路径,该如何为或或任何域索引工作?非常感谢。
/^\/[^/]+\/boards/.test(location.pathname)
if( /^\/[^/]+\/boards/.test(location.pathname) ) {
$("#container ul #all_categories a.categories-menu").css("font-weight","bold");
}
});