加载iframe和目标内容

Load iframe and target content

本文关键字:目标 iframe 加载      更新时间:2023-09-26

我有一个脚本,可以将iframe写入页面。我想把这个iframe中的一个链接作为一个函数。问题是:在编写iframe之后,我使用以下代码:

$('#iframe_pin').load(function () {
            $(this).contents().find('a.close').click(function (event) {
                event.preventDefault();
                $('.loaded').fadeOut(fade_time).remove();
            })
        })

但它不起作用。但如果我在加载功能后发出警报,比如这样:

$('#iframe_pin').load(function () {alert('bla bla');
            $(this).contents().find('a.close').click(function (event) {
                event.preventDefault();
                $('.loaded').fadeOut(fade_time).remove();
            })
        })

它有效!

当然,我不能将警报保持在代码中:)

有人能帮我吗?

从评论中,您可以得到以下内容:

<div class="loaded">
    <iframe id="iframe_pin" src="abc.html" width="100%" height="100%">
</div>

所以,你所需要做的就是:

$("#iframe_pin").load(function() {
    // let's get the iframe source
    var iframe = $("#iframe_pin").contents();
    iframe.find("a.close").bind("click", function() {
        $(".loaded").fadeOut('slow', function() {
            // now that the FadeOut is finished, let's remove
            $(".loaded").remove();
        });
        return false; // let's prevent to jump in the click
    });
});

在我自己的空间工作的例子


示例2通过创建<iframe>动态


示例3通过动态创建<iframe>,移除它们并重新创建