0

是否有代码片段可以检测当前用户的页面,然后添加<a class="active">ul(我的菜单)中的项目。我正在制作一个 tumblr 主题,但它们不允许 PHP(我通常<?php if ( $current == "home" ) { echo "class='active'" } ?>在我的菜单和$current = "home"页面中添加),而且它们没有标准(例如 current_page_item),所以我只需要使用 javascript(或 jQuery)来执行此操作。可能吗?

4

1 回答 1

0

It's not possible with HTML only but should be straight forward with JQuery, and just a bit harder with javascript only.

Here is a solution relying on JQuery and URL parser plugin: assuming you have the information you need in the file name (e.g: page1.html refered by id="page1")

$(document).ready(function() {
   var pathname = window.location.pathname;
   var page = jQuery.url.attr('file').replace(".html", "");
   $('#' + page).addClass("active");
});
于 2010-09-14T02:36:15.670 回答