How can I call a function after an element has been created in jquery?

前端 未结 9 2302
野趣味
野趣味 2020-12-28 13:37

I want to call a function after an element has been created. Is there a way to do this?

Example:

$(\"#myElement\").ready(function() {
    // call the         


        
9条回答
  •  星月不相逢
    2020-12-28 14:27

    You can use setInterval function to check the existence of an element. Once the function runs, you can clear the interval:

    var CONTROL_INTERVAL = setInterval(function(){
        // Check if element exist
        if($('#some-element').length > 0){
            // ...
            // Since element is created, no need to check anymore
            clearInterval(CONTROL_INTERVAL);
        }
    }, 100); // check for every 100ms
    

提交回复
热议问题