无法触发 iframe 加载/就绪事件 (jquery) 上的操作

发布于 2025-01-03 10:09:36 字数 2437 浏览 0 评论 0 原文

我有一个管理面板,我想在其中上传与产品相关的图片。我已将上传到 iframe 和图片预览分离到另一个 iframe,这样我就不必在每次提交表单时重新加载整个页面。现在它看起来像这样(添加边框以供参考):

在此处输入图像描述

我想:

  • 由于高度两个 iframe 的高度都是可变的(取决于第二个 iframe 的图片数量,上传后会在第一个 iframe 上显示反馈消息):设置 iframe 的高度以适合正文内容的高度。
  • 提交上传表单后显示进度条 gif,并在框架加载完成后隐藏它。
  • 表单提交后上传表单加载完成后重新加载预览框架,以便显示已添加的新图片。

我的代码现在看起来像这样:

HTML:

<p>
Subir imagen:
<span class="loading_bar"></span><br />
<iframe name="upload_iframe" class="upload_iframe" src="upload.php" scrolling="no"></iframe>
</p>

<p>
<iframe name="slideshow_iframe" class="slideshow_iframe" src="slideshow.php" scrolling="no"></iframe>
</p>

Javascript:

//<!--
$(document).ready(function(){
    // ------------------ UPLOAD ------------------

    // On any frame load
    $("iframe").load(function() {
        // Change frame's height to be the same as the contents body's height
        var iframe_height = $(this).contents().find('body').height();
        $(this).height(iframe_height);
        // Control point
        alert("Frame: "+$(this).attr("class")+" | Content height: "+iframe_height+" | Frame height: "+$(this).height());
    });

    // On "iframe_upload" load
    $("iframe.upload_iframe").load(function() {
        // Hide progress bar
        $(this).prev("span.loading_bar").hide();
        // Reload "iframe_slideshow"
        $("iframe.upload_slideshow").attr("src",$("iframe.upload_slideshow").attr("src"));
    });

    // On "select file" change
    $(".upload_form_upload").change(function() {
        var parent_element = "#"+$(this).closest("form").attr("id");
        // Show progress bar
        $(parent_element+" span.loading_bar", parent.document).show();
        // Submit upload form
        $(this).closest("form").submit();
    });
}); 
//--> 

现在的麻烦是:

  • 加载下的操作永远不会触发,不会在网站首次加载时触发,也不会在提交表单时触发。
  • 如果我将 .load() 更改为 .ready(),当网站加载 $("iframe").ready() { 中的所有内容时,每个 iframe 以及 $("iframe.upload_iframe").ready 下的内容都会触发两次() 永远不会被触发。即使我收到警报,新的高度也没有设置。
  • 这是我从“控制点”下的警报中得到的信息:

页面加载时,第一次:

>页面加载,第二次:

在此处输入图像描述

提交后:

在此输入图像描述

I have an admin panel where I want to upload pictures associated to a product. I have separated the upload to an iframe and the picture preview to another iframe so I don't have to reload the entire page every time a form is submited. Right now it looks like this (added border for reference):

enter image description here

I'd like to:

  • Since the height of both iframes is variable (it depends on the quantity of pictures in the second, and a feedback message is shown on the fist after an upload has been made): set the height of the iframe to fit the height of the body content.
  • Show a progress bar gif after the upload form has been submitted, and hide it when the frame has finished loading.
  • Reload the preview frame after the upload form has finished loading after a form submit, so it shows the new picture that has been added.

My code looks like this right now:

HTML:

<p>
Subir imagen:
<span class="loading_bar"></span><br />
<iframe name="upload_iframe" class="upload_iframe" src="upload.php" scrolling="no"></iframe>
</p>

<p>
<iframe name="slideshow_iframe" class="slideshow_iframe" src="slideshow.php" scrolling="no"></iframe>
</p>

Javascript:

//<!--
$(document).ready(function(){
    // ------------------ UPLOAD ------------------

    // On any frame load
    $("iframe").load(function() {
        // Change frame's height to be the same as the contents body's height
        var iframe_height = $(this).contents().find('body').height();
        $(this).height(iframe_height);
        // Control point
        alert("Frame: "+$(this).attr("class")+" | Content height: "+iframe_height+" | Frame height: "+$(this).height());
    });

    // On "iframe_upload" load
    $("iframe.upload_iframe").load(function() {
        // Hide progress bar
        $(this).prev("span.loading_bar").hide();
        // Reload "iframe_slideshow"
        $("iframe.upload_slideshow").attr("src",$("iframe.upload_slideshow").attr("src"));
    });

    // On "select file" change
    $(".upload_form_upload").change(function() {
        var parent_element = "#"+$(this).closest("form").attr("id");
        // Show progress bar
        $(parent_element+" span.loading_bar", parent.document).show();
        // Submit upload form
        $(this).closest("form").submit();
    });
}); 
//--> 

Now the trouble:

  • The actions under load never trigger, not when the website is first loaded, nor when the form is submitted.
  • If i change .load() for .ready(), when the website is loaded everything inside $("iframe").ready() { triggers twice for each iframe and what is under $("iframe.upload_iframe").ready() is never triggered. Even though I get to the alert, the new height is not set.
  • This is what I get from the alert under "Control Point":

On page load, first time:

enter image description here

On page load, second time:

enter image description here

After submit:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽容 2025-01-10 10:09:36

这是我的 iframe 调整大小脚本,可以跨浏览器工作。希望有帮助!

//get iframe
var iframe = $('iframe')[0];
//get iframe body (IE?)        
var iframeBody = (iframe.contentDocument) ? iframe.contentDocument.body : iframe.contentWindow.document.body;
//works out the new height by matching the offsetHeight and scrollHeight
var height = (iframeBody.scrollHeight < iframeBody.offsetHeight) ? iframeBody.scrollHeight : iframeBody.offsetHeight;
//sets the new height on the iframe
$(iframe).height(height); 

This is my iframe resize script which works cross browser. Hope it helps!

//get iframe
var iframe = $('iframe')[0];
//get iframe body (IE?)        
var iframeBody = (iframe.contentDocument) ? iframe.contentDocument.body : iframe.contentWindow.document.body;
//works out the new height by matching the offsetHeight and scrollHeight
var height = (iframeBody.scrollHeight < iframeBody.offsetHeight) ? iframeBody.scrollHeight : iframeBody.offsetHeight;
//sets the new height on the iframe
$(iframe).height(height); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文