根据路径名将类添加到元素

Add Class to element according to pathname

本文关键字:添加 元素 路径名      更新时间:2023-09-26

此脚本非常适合将类"crime"添加到此页面名为"h1_wrapper"的div中:

if(window.location.pathname == '/Criminal-Law-Blog') {$( ".h1_wrapper" ).addClass( "criminal" );

当我点击进入特定的博客条目时,它会添加到路径名中;例如:

/刑法-博客/

2014/11/刑法-博客-测试-入口-1

如何编辑代码以使其将类添加到"/Crime-Law-Blog"之后的任何路径?我知道这可能很容易,但我就是想不通!

提前感谢!

最简单的方法是做window.location.pathname.match(/^'/Criminal-Law-Blog/)