Sweet Alert 2 在父框架中打开

2024-03-05

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

以下目标选项均无效:

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

这是当“body”为目标时得到的结果:

Edit:

SweetAlert 2 代码

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

i框架代码

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

您好,您可以做的是将 sweetalert2 代码放在您的父窗口中。然后在 iframe 中使用 JavaScript 的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>

内嵌框架 HTML

<!DOCTYPE html>
<html>
<body>
     <button onclick="foo();">Click Me</button>
     <script>
     function foo(){
         parent.postMessage("openAlert", "*");
     }
     </script>
</body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Sweet Alert 2 在父框架中打开 的相关文章