javascript - 加载 iframe 和目标内容

标签 javascript jquery iframe jquery-events dom-manipulation

我有一个将 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
    });
});

working example at my own space


Example 2 by creating the <iframe> on the fly


Example 3 by creating the <iframe> on the fly, removing them and re-creating them

关于javascript - 加载 iframe 和目标内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6263535/

相关文章:

javascript - 无法使用 Three.js

javascript - 使用删除功能将自动完成 TextView 中的选定值添加到 div

jQuery 可拖动输入元素

jquery - 如何使用 JQuery 将点击事件添加到 iframe

jquery - YouTube iFrame 打破 jQuery

javascript - 将元素绑定(bind)到不同框架中的现有 AngularJS 范围

javascript - 无法在 writeFile 方法中传递对象

javascript - jQuery 显示/隐藏所需的密码字段

javascript - 简单的 HTML5 放置文件站点网站,它压缩放置的文件

jquery - 如果下拉列表可见,则将类添加到父级 li