execute functions after a dynamically created div's content(like images) is loaded, using jQuery

余生长醉 提交于 2019-12-24 20:11:53

问题


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.

The below is not working.

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

This also not working.

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

Please Help! :(


回答1:


Will this do it?

jQuery .load()

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

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

A working example:

<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>


来源:https://stackoverflow.com/questions/13031064/execute-functions-after-a-dynamically-created-divs-contentlike-images-is-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!