在使用jQuery加载动态创建的div的内容(如图像)之后执行函数 [英] execute functions after a dynamically created div's content(like images) is loaded, using jQuery

查看:184
本文介绍了在使用jQuery加载动态创建的div的内容(如图像)之后执行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有在动态创建DIV的内容(包括文本和图像)完整加载之后,我才想执行某些功能。这里动态创建的DIV是#element+数据。

i want to execute some functions only after a dynamically created DIV's content (which will includes text and images) is loaded completely. Here the dynamically created DIV is "#element"+data.

以下是无效的。

$("#section"+currSect).delegate( "#element"+data, "load", function()
{ remvPanel(); showFormBox(); }
);

这也不行。

 $("#element"+data).load(function() {remvPanel(); showFormBox();});

请帮忙! :(

推荐答案

这样做吗?

jQuery .load()

此方法是一个快捷方式.bind('load',handler)。

This method is a shortcut for .bind('load', handler).

当事件和所有子元素已经完全加载时,加载事件被发送到一个元素。可以发送到与URL相关联的任何元素:图像,脚本,框架,iframe和窗口对象。

The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.

例如,考虑具有简单图像的页面:

For example, consider a page with a simple image:

<img src="book.png" alt="Book" id="book" />

事件处理程序可以绑定到图像:

The event handler can be bound to the image:

$('#book').load(function() {
  // Handler for .load() called.
});

一个工作示例:

<head>
    <title></title>
    <script src="Scripts/jquery-1.8.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $div = $("#main");

            // get loaded alert
            $img = $("<img src='https://www.google.com/images/srpr/logo3w.png' />");

            // no loaded alert
            // $img = $("<img src='not a valid image' />");

            $img.load(function () {
                alert("loaded");
            });

            $div.append($img);
        });
    </script>
</head>
<body>
    <div id="main">
    </div>
</body>

这篇关于在使用jQuery加载动态创建的div的内容(如图像)之后执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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