将类添加到指定的元素

Jul*_*eto 1 javascript php jquery

好的,我有这个代码

//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."' />
Run Code Online (Sandbox Code Playgroud)

现在,我想将一个类'active'添加到与属性'page'的值匹配的元素到id为'currentpage'的隐藏输入字段的值,为了做到这一点,我更喜欢在jquery上创建它所以继承代码.

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

我不知道如何在隐藏字段的值上添加一个类,该类具有其属性"page"的匹配值,该字段的id为"currentpage"

例如:

<input type='hidden' id='currentpage' value='home' />
Run Code Online (Sandbox Code Playgroud)

所以基于元素的attr'page'应该添加一个活动类,所以这应该是这样的.

<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>
Run Code Online (Sandbox Code Playgroud)

希望有人能给我一些有助于解决当前问题的事情,在此先感谢.

PS:我在想法,建议和建议中公开.

Aus*_*tin 5

像这样:

$("a[page="+$page+"]").addClass("active");
Run Code Online (Sandbox Code Playgroud)


Ohg*_*why 5

 $('a[page='+$page+']').addClass('active');
Run Code Online (Sandbox Code Playgroud)

的jsfiddle