gpt4 book ai didi

javascript - 如何将类添加到通过 jquery 加载的 html 文件?

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

我使用 jQuery 中的 .load() 将页眉和页脚包含在单独的 html 文件中。我的 jQuery 代码如下:

$(function(){
$("#header").load("page-component/header.html");
$("#footer").load("page-component/footer.html");
});

现在,当我导航到另一个页面时,我想将一个 active 类添加到我的页眉/页脚链接之一,但我似乎无法实现。谁能帮帮我?

我试过类似的方法,但没有用:

$(document).ready(function(){
jQuery('#menu-about').addClass('active');
});

最佳答案

使用jQuery.load完成回调方法。

.load( url [, data ] [, complete ] )

当你为元素调用addClass方法时,元素DOM中不存在,回调函数是当外部文件被加载到指定的元素

时调用
$(function() {
$("#header").load("page-component/header.html", function() {
jQuery('#menu-about').addClass('active');
});
$("#footer").load("page-component/footer.html", function() {
jQuery('#menu-about').addClass('active');
});
});

关于javascript - 如何将类添加到通过 jquery 加载的 html 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39738519/

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