且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-03 16:45:46

使用 jQuery.load 方法.

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

当调用元素的addClass方法时,DOM中不存在element,当指定的element中的external文件为loaded时,调用callback函数.

When you are invoking addClass method for element, element does not exist in DOM, callback function is invoked when external file is loaded in specified element

$(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');
  });
});