简体   繁体   中英

Run a script after knockout has finished

I have a front-end script that alerts when a link is clicked. The problem is, the link that my script looks for is loaded on the page via a knockout template, and I can't get my script to run after that happens.

I am not able to execute this within knockout, unfortunately, it must be done within my front-end CMS. Not ideal, I know.

Is there any way to work around knockout and ensure my script runs AFTER the knockout template has finished loading? I got it to work with a timeout, but that's not ideal. I've also tried docready, onload, etc, and no dice.

My script is very simple, I'm wondering if there is something specific to knockout that I can wrap it in...

  $('.myLink').on('click', function(event) {
  var foo  = $(this).text();
  alert(foo);
});

You could attach the event handler to an ancestor that does exist on the page on page load and create a delegated event handler :

$(document.body).on('click', '.myLink', function (event) {
    /* ... */
});

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