美文网首页
load加载html之后$()无法选择元素问题

load加载html之后$()无法选择元素问题

作者: 不知道取个什么昵称不如娶个媳妇 | 来源:发表于2018-10-09 17:21 被阅读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();
   });
});

相关文章

网友评论

      本文标题:load加载html之后$()无法选择元素问题

      本文链接:https://www.haomeiwen.com/subject/itdyaftx.html