将类添加到指定的元素

将类添加到指定的元素

问题描述:

好的,我有这个代码

//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."' />

现在,我想向与ID为"currentpage"的隐藏输入字段的值匹配的,将属性"page"的值匹配的元素中添加一个活动"类,以便做到这一点,我更喜欢在jquery上使用它,因此代码在这里.

now, i want to add a class 'active' to the element that match the value of the attribute 'page' into the value of the hidden input field which has the id of 'currentpage', in order to do that, i prefer to make it on jquery so heres the code.

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

我不知道如何从ID为'currentpage'的隐藏字段的值中,添加具有其属性'page'的匹配值的指定元素上的类

I dont know how to add a class on the specified element that has a match value of its attribute 'page' from the value of the hidden field which has an id of 'currentpage'

例如:

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

因此,应基于元素的attr页面"添加一个活动类,因此应该像这样.

so base on the attr 'page' of the element an active class should be added, so this should be like this.

<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>

希望有人能给我一些可以帮助解决当前问题的信息,

Hope someone could give me something that could help to solve this current issue, thanks in advance.

PS:即时交流思想,建议和建议.

PS: im open in ideas, suggestions and recommendations.

 $('a[page='+$page+']').addClass('active');

jsfiddle