如何将类添加到通过jquery加载的html文件中?
我已经包含了标头&使用jQuery中的.load()
在单独的html文件中添加页脚.我的jQuery代码如下:
I've included my header & footer at separate html files using .load()
from jQuery. My jQuery code is below:
$(function(){
$("#header").load("page-component/header.html");
$("#footer").load("page-component/footer.html");
});
现在,当我导航到另一页时,我想在我的页眉/页脚链接之一中添加一个active
类,但似乎无法实现.有人可以帮助我吗?
Right now, as I navigate to another page, I want to add an active
class to one of my header/footer links but I can't seem to achieve that. Anyone can help me?
我已经尝试过类似的方法,但是没有用:
I've tried something like this but it didn't work:
$(document).ready(function(){
jQuery('#menu-about').addClass('active');
});
使用 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');
});
});