2016-09-28 92 views
1

我已经使用来自jQuery的​​在单独的html文件中包含我的标题&页脚。我的jQuery代码如下:如何将类添加到通过jquery加载的html文件?

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

现在,当我浏览到另一个网页,我想一个active类添加到我的页眉/页脚环节之一,但我似乎无法做到这一点。任何人都可以帮助我?

我已经试过这样的事情,但它没有工作:

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

回答

3

使用jQuery.load方法complete callback

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

当你调用的元素addClass方法,element不存在DOMcallback函数被调用时external文件中指定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'); 
    }); 
}); 
+0

感谢loaded ,这工作。 –

+1

@JoshuaRajandiran - 我很高兴它有帮助! _快乐编码_ – Rayon

相关问题