1

好的,我有这个代码

//start our session
session_start();
//check if there is a page request
if (isset($_GET['page']))
{
//if there is a request page then get it and put it on a session variable and sent back the data that was requested.
$_SESSION['page'] = $_GET['page'];
$currentpage = $_SESSION['page'];
}
//if there is no request then..
else
{
$_SESSION['page'] = "home"; //if there is no request page then we literally tell that we are in the main page so we session will be home
$currentpage = $_SESSION['page'];
}

//echo a hidden input fields that hold the value of current page which will be accessed  by the jquery for adding classes of the match value in the attribute 'page' of the element base on the value that the hidden input field has

echo "<input type='hidden' id='currentpage' value='".$currentpage."' />

现在,我想将一个类 'active' 添加到与属性 'page' 的值匹配的元素中,并将其添加到 ID 为 'currentpage' 的隐藏输入字段的值中,为了做到这一点,我更喜欢在 jquery 上制作,所以代码如下。

$(document).ready(function(){
//get the value of the hidden input field
$page = $('#currentpage').val();
//now im stuck here ..
});

我不知道如何从 id 为“currentpage”的隐藏字段的值中在指定元素上添加一个类,该元素的属性“page”匹配值

例如:

<input type='hidden' id='currentpage' value='home' />

所以基于元素的属性“页面”应该添加一个活动类,所以应该是这样的。

<ul>
    <li><a page="home" href="index.php" class="active">home</a></li> <!--as you can see on this part an active class has been added thats because the value of the attr page of this element is match to the value of the hidden input fields which has the id of 'currentpage'-->
    <li><a page="gallery" href="index.php?page=gallery">gallery</a></li>
    <li><a page="about" href="index.php?page=about">about</a></li>
    <li><a page="contact" href="index.php?page=contact">contact</a></li>
</ul>

希望有人能给我一些可以帮助解决当前问题的东西,在此先感谢。

PS:我对想法、建议和建议持开放态度。

4

2 回答 2

5

像这样:

$("a[page="+$page+"]").addClass("active");
于 2012-07-22T03:02:34.027 回答
5
 $('a[page='+$page+']').addClass('active');

jsfiddle

于 2012-07-22T03:03:21.597 回答