你可以在 PHP 中有这个:
<?php
//$url = 'http://example.com/path1/path2/?id=9238293&name=test';
$url = $_POST['path'];
//REMOVE THE LAST PARAMETER IN URL ?id=9238293&name=test';
$paths = explode('?',$url);
$paths = count($paths) ? $paths[0] : $paths; //if have parameter or not
$paths = explode('/', $paths); //SEPARATE THE URL
if (!$paths[count($paths)-1]){
unset($paths[count($paths)-1]); //VERIFY IF THE URL ENDS WITH '/'
}
$count = count($paths);
//THEN YOU HAVE
//YOUR CURRENT PAGE
//print "Current page: ".$paths[$count-1];
//print "Mother page: ".$paths[$count-2];
//THEN YOU CAN COMPARE THE MOTHER PAGE WITH ONE EXISTING MENU PAGE
//IN YOUR EXAMPLE YOU CAN GO:
$return = array();
$site = "http://example.com/";
$current = $paths[$count-1];
$mother = $paths[$count-2];
$return['current'] = $site.$mother.'/'.$current; //THAT WAY YOU STRIP THE PARAMETERS TO GET THIS ON YOUR JAVASCRIPT
$return['mother'] = $site.$mother;
print json_encode($return);exit;
?>
然后在你的客户端做:
<script>
jQuery(document).ready(function($){
var path = window.location;
//very important!
path = path.toString();
$.post('YOUR PHP SCRIPT URL',{path:path},function(data){
$('#nav a[href="'+data.current+'"]').addClass('current');
$('#nav a[href="'+data.mother+'"]').addClass('current');
},'json');
});
</script>