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

查看:174
本文介绍了如何选择通过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天全站免登陆