无法在按钮点击时关闭iframe

时间:2013-07-09 20:10:47

标签: javascript iframe

您好我有一张图片,点击该图片后,它会调用另一个以.rame格式加载的.aspx页面。我想点击iframe内的按钮关闭iframe。我尝试了不同的方法,但它们都没有工作:

这是我的iframe:

function openBox() {
    $(".nodeOption, .nodeOptionBlack").fadeIn(200);
    var pageHeight = $(document).height();
    var pageWidth = $(document).width();
    $(".nodeOptionBlack").css("height", pageHeight).css("width", pageWidth);
}

$(document).ready(function() {
    $(".nodeOptionBlack").click(function () {
        $(".nodeOption, .nodeOptionBlack").fadeOut(200);
    });

    $(".WindowClose").click(function () {
        $(".nodeOption, .nodeOptionBlack").fadeOut(200);
    });

Here is my function of button click:

function Submit_Click() {
    var enroll = test();
    if (enroll == "sale") {
        a.Node(urlParams["parentid"], urlParams["placement"], 1, "Sale");
    }
    else {
        a.Node(urlParams["parentid"], urlParams["placement"], 1, "");
    }

//here i want to close my iframe 
}

i tried different things like:

  $("#nodeOption").remove();
  window.parent.closeIframe();

      //document.getElementById("nodeOption").parent.removeChild(iframe);
     //$('#nodeOption', top.document).dialog('close');
     // window.parent.closeIframe();
    //$(".nodeOption, .nodeOptionBlack").fadeOut(200);

function closeIframe() {
    alert("hi");
    var ifram = document.getElementById(".nodeOption");
    iframe.parent.removeChild(iframe);
}

请建议我做错了什么。谢谢!

2 个答案:

答案 0 :(得分:1)

解决方案

由于您使用的是jQuery,因此请使用元素的.remove()方法。在这里,我们获取带有id“iframe”的iframe,并通过点击任意button元素将其从正文中删除:

的JavaScript / jQuery的

/* This part is the put the button inside the iframe,
   it's not related to your problem */

$('#iframe').contents().find('body').append('<button>Click Here</button>');

/* We seek for a `button` element within the iframe body, in the iframe content */
$('#iframe').contents().find('body button').click(function (event) {
    event.preventDefault();
    $('#iframe').remove();
    $('#result').text('Iframe deleted!');
});

哦,相信我,你不想在Pure JavaScript中做到这一点(在你试图让iframe的ownerDocument删除iframe元素之前,一切都很简单。)

Live Demo (更新)

答案 1 :(得分:1)

感谢您的帮助,我的问题得到了解决。我在这里做了什么:

在我的iframe按钮上单击我调用

 window.parent.closeIframe1();

在父页面上我编码:

  function closeIframe1()
        {
            $(".nodeOption, .nodeOptionBlack").fadeOut(200);
            return false;
        }

它对我来说很好。