简体   繁体   中英

When I run the jQuery command the element doesn't exists

I would like to modify the dom with jquery, but when I run the command, the dom element doesn't exist. Is it possible to wait for the other script to build the necessary html elements?

I tried with the 'live' function, but doesn't help.

jQuery(document).live('ready', function(){

        var pressTitle;

        jQuery('.press-page .soliloquy-item').each(function(){

        pressTitle = jQuery(this).children().attr('title');

        console.log(pressTitle);

        });

    });

How can I fix this?

You can check if you find any elements, and if you don't, wait for a while and try again:

function getTitle() {
  var pressTitle;
  var t = jQuery('.press-page .soliloquy-item');
  if (t.length) {
    t.each(function(){
      pressTitle = jQuery(this).children().attr('title');
      console.log(pressTitle);
    });
  } else {
    window.setTimeout(getTitle, 100);
  }
}

jQuery(document).ready(getTitle);

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