gpt4 book ai didi

php - 将类添加到指定元素

转载 作者:行者123 更新时间:2023-11-30 07:09:59 24 4
gpt4 key购买 nike

好的,我有这个代码

//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”的值匹配到具有“currentpage”id 的隐藏输入字段的值中,为了做到这一点,我更喜欢在 jquery 上制作它,所以这是代码。

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

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

例如:

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

所以基于元素的 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>

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

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

最佳答案

像这样:

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

关于php - 将类添加到指定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11597489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com