2012-07-19 34 views
0

我目前使用这个javascript函数:菜单选项卡/主动式的根据在URL中的file.php,

<a href="t1.php" class="<?php if(end(explode("/", $_SERVER["PHP_SELF"])) == "t1.php") { echo 'active'; } else {} ?>">Tab2</a> 
<a href="t2.php" class="<?php if(end(explode("/", $_SERVER["PHP_SELF"])) == "t2.php") { echo 'active'; } else {} ?>">Tab2</a> 

但我不喜欢用PHP这样的,我想我浪费时间处理这在PHP服务器端。 基本上我想在javaScript中做到这一点,当页面加载或加载时,JavaScript函数trigers和更改类取决于如果url中的file.php。另外,请避免所有JQuery脚本/插件和所有类型的东西。

也许有另一种方法可以有效地做到这一点?

希望我解释清楚。 〜谢谢,我是人类。

回答

0

如果你可以添加到这些元素的ID ATTR(只是为了更容易操纵),你可以做这样的事情(附脚本身体的onload或在脚本的末尾):

<a href="t1.php" id="tab1">Tab2</a> 
<a href="t2.php" id="tab2">Tab2</a> 

JavaScript:

var ThisLocation = document.location.href; 
if(ThisLocation.indexOf('t1.php') !== -1) 
{ 
    document.getElementById('tab1').setAttribute('class', 'active'); 
} 
else if(ThisLocation.indexOf('t2.php') !== -1) 
{ 
    document.getElementById('tab2').setAttribute('class', 'active'); 
}