简体   繁体   中英

How to add jQuery code after js snippet is fully loaded?

I'm having trouble adding click function to the button which is loaded with JS snippet, because when I try doing the function like this on document.ready

jQuery(document).ready(function( $ ){   
    $(".esis__heading").on('click', function(event){
      console.log('123123');
})});

it fails because the element is not loaded and visible yet.

I was thinking maybe timeout is a solution but I started to think it's not a perfect solution. If someone has any advice on how to deal with this it would help a lot. This is the link to a page which has a snippet that loads after some time https://samsung.plutonium.rs/samsung/

Thanks in advance.

You solve this by delegating the click to a parent element, at worst this can be the document itself

$(document).ready(function( $ ){   
    $(document).on('click', ".esis__heading", function(event){
      console.log('123123');
    });
});

You can defer the load of the script using the defer attribute

Defer documentation (w3schools.com)

The only downside is that it works only on external scripts (script tags with src attribute).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM