load加载html之后$()无法选择元素问题
2018-10-09 本文已影响14人
不知道取个什么昵称不如娶个媳妇
var Load = function(){
this.htmlLoad();
}
Load.prototype = {
htmlLoad:function(){
$("header").load("./header.html");
$("footer").load("./footer.html");
},
showList:function(){
console.log($(".color1"));//length:0
}
}
new Load();
原因是load加载html之前showlist就已经执行完了,所以选择不到
$("header").load("./header.html",$.proxy(this.showList,this));
或者使用$.get("./header.html",$.proxy(this.showList,this))
require(["config"],function(){
require(["jquery"],function($){
var Load = function(){
this.htmlLoad();
}
Load.prototype = {
htmlLoad:function(){
$("header").load("./header.html",$.proxy(this.showList,this));
// $.get("./header.html",$.proxy(this.showList,this))
$("footer").load("./footer.html");
},
showList:function(){
// console.log($(".color1").next());
$(".color1,.color2").mouseenter(function(){
$(this).next().fadeIn();
})
$(".color1,.color2").mouseleave(function(){
$(this).next().fadeOut();
})
$(".megapanel").mouseenter(function(){
$(this).stop().fadeIn();
})
$(".megapanel").mouseleave(function(){
$(this).fadeOut();
})
}
}
new Load();
});
});