如何选择通过 jQuery load() 函数加载的元素? [英] How to select an element loaded through the jQuery load() function?

查看:34
本文介绍了如何选择通过 jQuery load() 函数加载的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到以下问题(首先是一些示例代码):

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.

我知道 live() 方法,但我不想想将我的动作绑定到任何事件,即我只想用这些新的动作运行一些动作元素无需点击、聚焦、模糊等

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.

推荐答案

load 函数是异步的.
您的下一行在内容加载之前运行.

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

你需要把你的代码放在load函数的回调中,这样它只会在新内容加载后运行:

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();
});

这篇关于如何选择通过 jQuery load() 函数加载的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆