How to wait for div to load before calling another function?

后端 未结 3 976
猫巷女王i
猫巷女王i 2021-01-01 17:25


        
相关标签:
3条回答
  • 2021-01-01 17:51

    Use load callback

    $("#tempMain").load($(this).attr("href"),function(){
       resizeDivs();
       // do other stuff load is completed
    });
    
    0 讨论(0)
  • 2021-01-01 17:54

    with jquery

    function waitForElement(elementPath, callBack){
      window.setTimeout(function(){
        if($(elementPath).length){
          callBack(elementPath, $(elementPath));
        }else{
          waitForElement(elementPath, callBack);
        }
      },500)
    }
    

    e.g. to use:

    waitForElement("#myDiv",function(){
        console.log("done");
    });
    

    here is without jquery

    function waitForElement(elementId, callBack){
      window.setTimeout(function(){
        var element = document.getElementById(elementId);
        if(element){
          callBack(elementId, element);
        }else{
          waitForElement(elementId, callBack);
        }
      },500)
    }
    

    e.g. to use:

    waitForElement("yourId",function(){
        console.log("done");
    });
    
    0 讨论(0)
  • 2021-01-01 18:08

    You can attach a callback in the jQuery load function :

    $("#tempMain").load($(this).attr("href"), resizeDivs);
    

    see : http://api.jquery.com/load/

    0 讨论(0)
提交回复
热议问题