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>