将类添加到指定的元素
好的,我有这个代码
//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 "
现在,我想将一个类’active’添加到与属性’page’的值匹配的元素到id为’currentpage’的隐藏输入字段的值,为了做到这一点,我更喜欢在jquery上创建它所以inheritance代码。
$(document).ready(function(){ //get the value of the hidden input field $page = $('#currentpage').val(); //now im stuck here .. });
我不知道如何在隐藏字段的值上添加一个类,该类具有其属性“page”的匹配值,该字段的id为“currentpage”
例如:
所以基于元素的attr’page’应该添加一个活动类,所以这应该是这样的。
希望有人能给我一些有助于解决当前问题的事情,在此先感谢。
PS:我在想法,建议和建议中公开。
像这样:
$("a[page="+$page+"]").addClass("active");
$('a[page='+$page+']').addClass('active');
的jsfiddle