gpt4 book ai didi

javascript - 使用 jquery 或 javascript 更改 html 中类属性的值

转载 作者:行者123 更新时间:2023-11-27 23:54:03 25 4
gpt4 key购买 nike

我想更改这一行:

    <li id="activehome" ><a href="#" >Home</a></li>

    <li id="activehome" class="active"><a href="#" >Home</a></li> 

添加该类属性。我如何使用 Javascript 甚至 Jquery 来做到这一点。提前致谢。页面加载时应更改该行。

最佳答案

jQuery,使用 addClass :

$('#activehome').addClass('active');

纯JS,使用setAttribute :

document.getElementById('activehome').setAttribute('class', 'active');

或使用className

document.getElementById('activehome').className = 'active';

关于javascript - 使用 jquery 或 javascript 更改 html 中类属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32420177/

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