我们想根据window.location.pathname在链接上添加类
我们的html是:
<nav>
<a href="index.php">Home</a>
<a href="company.php">Company</a>
<a href="products.php">Products</a>
<a href="technical.php">Technical</a>
<a href="services.php">Services</a>
<a href="contactus.php">Contact Us</a>
</nav>
并且文档 URL 是www.nicadpower.com/index.php所以我们做了
var page_name = window.location.pathname.substring(1);
在获取/知道路径名是'index.php'之后,我们希望 jquery 能够将'page_name'与href进行比较,如果它相等,则添加一个class="current"
让我们的html代码变成这样
<nav>
<a href="index.php" class="current">Home</a>
<a href="company.php">Company</a>
<a href="products.php">Products</a>
<a href="technical.php">Technical</a>
<a href="services.php">Services</a>
<a href="contactus.php">Contact Us</a>
</nav>