甜蜜警报 2 在父框架中打开

Sweet Alert 2 Open in Parent Frame

我有一个 iframe,其源是一个 php 文件。我想要做的是让 Sweet Alert 2 在父框架中打开,而不是在 iframe 内部打开。我试过改变目标,但没有成功。

None 以下目标选项有效:

swal({
    target: 'window'
    target: 'document'
    target: 'parent'
});

这是我在 'body' 为目标时得到的结果:

编辑:

SweetAlert 2 代码

swal({
    type: 'error',
    text: 'You did not select any files.',
    target: 'top'
});

iFrame代码

<div id="viewWindow" class="col-xs-10 content">
    <iframe id="waiverList" src="php/edit_archive.php"></iframe>
</div>

您好,您可以将 sweetalert2 代码放入 parent window。然后在您的 iframe 中使用 JavaScript 的 parent 属性 来调用该函数。为避免出现同源策略错误,您可以使用 window post 消息。

像这样:

Parent Html

<!DOCTYPE html>
<html>
<body>
<script> 
    function openAlert() {
        swal(
            'Good job!',
            'You clicked the button!',
            'success'
        )
    }
    window.addEventListener("message", function(event) {
        if(event.data == "openAlert");{
            openAlert();
        }
    });

</script>
    <iframe src="iframe.html">

    </iframe>
</body>
</html>

iframe HTML

<!DOCTYPE html>
<html>
<body>
     <button onclick="foo();">Click Me</button>
     <script>
     function foo(){
         parent.postMessage("openAlert", "*");
     }
     </script>
</body>
</html>