开发者

How to select an element loaded through the jQuery load() function?

I am currently having trouble with the following (here is some sample code first):

<div id="container"></div>

<script type="text/javascript">
    $('#container').load('content开发者_开发问答.html');

    $('.elementInContentHTML').fadeIn();
</script>

In short, I want to be able to access elements that have been dynamically added to a page without attaching them to event handlers.

I know about the live() method, but I do not want to bind my action to any event, i.e. I just want to run some actions with these new elements without clicking them, focusing, blurring, etc.


The load function is asynchronous.
Your next line runs before the content is loaded.

You need to put your code inside the load function's callback, so that it will only run after the new content is loaded:

$('#container').load('content.html', function() {
    $('.elementInContentHTML').fadeIn();
});


You could try using the callback for when load completes? See http://api.jquery.com/load/

$('#result').load('ajax/test.html', function() {
  alert('Load was performed.');
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜