简体   繁体   English

加载页面后加载由脚本创建的Div。

[英]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. 如何在脚本加载和创建页面后加载div。

$(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. 我正在尝试加载由脚本创建的div。

Your click handler needs to go inside the DOM ready handler. 您的单击处理程序需要进入DOM就绪处理程序。 Without doing this, you will be trying to attach the click() to an element which doesn't yet exist. 如果不这样做,您将尝试将click()附加到尚不存在的元素。 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);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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