Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 加载iframe和目标内容_Javascript_Jquery_Iframe_Jquery Events_Dom Manipulation - Fatal编程技术网

Javascript 加载iframe和目标内容

Javascript 加载iframe和目标内容,javascript,jquery,iframe,jquery-events,dom-manipulation,Javascript,Jquery,Iframe,Jquery Events,Dom Manipulation,我有一个脚本,可以在页面中写入iframe。 我想在这个iframe中定位一个链接,使其具有一个函数。 问题是: 在编写iframe之后,我使用以下代码: $('#iframe_pin').load(function () { $(this).contents().find('a.close').click(function (event) { event.preventDefault(); $('.load

我有一个脚本,可以在页面中写入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>
工作示例


通过动态创建


通过动态创建
,删除它们并重新创建它们


你的html是什么?你能粘贴一个例子吗?我不知道什么是
#iframe\u pin
加载的
,其中一个是
?iframe\u pin是iframe;加载的是一个div;使用“.contents()”时,我将元素定向到iframe中,但除非我将警报放在“load”函数之后,否则它的行为就好像没有完全加载一样。因此:
?确切地说,我希望将“a.close”作为目标,以关闭包含iframe的div。此
.load
放置在哪里?--当DOM在父级中就绪时,应该立即设置它。仍然不起作用。我想这是因为iframe是在运行时使用另一个函数编写的。我的示例代码很有效,刚刚添加了第二个版本,所以您可以看到我正在动态创建
。如果您想感谢,请记住向上投票,如果您想要感谢,请回答正确,换句话说,如果答案是正确的。thnx balexandre,你的帖子运行得很好,我必须更好地检查我的代码,应该有一些错误。添加了第三个示例,你可以动态创建
,删除它并一次又一次地重新创建它。。。
$("#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
    });
});