简体   繁体   中英

Load Div created by script after page loaded.

How can I load a div after a page has been loaded and created by a script.

$(document).ready(function(){
   $('body').append('<div class="div_new" alt="hi">Hello World</div>');
});
$('.button').click(function(){
   alt_value = $('.div_new').attr('alt');
   alert(alt_value);
});

I'm trying to load a div created by a script.

Your click handler needs to go inside the DOM ready handler. Without doing this, you will be trying to attach the click() to an element which doesn't yet exist. Try this:

$(document).ready(function(){
    $('body').append('<div class="div_new" alt="hi">Hello World</div>');

    $('.button').click(function(){
        alt_value = $('.div_new').attr('alt');
        alert(alt_value);
    });
});

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