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

标签 jquery html sweetalert2

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

以下目标选项均无效:

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

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

enter image description here

编辑:

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 代码放在父窗口中。然后在您的 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>

iframe HTML

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

关于jquery - 甜蜜警报 2 在父框架中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47381063/

相关文章:

javascript - 防止 <label></label> 调整大小

javascript - svg动画逐个字母填充vivus.js

javascript - 将 div 动画到另一个 div 的右侧

html - 如何设置 Highslide 扩展器尺寸?

javascript - 如何在不使用 Promise 的情况下重写 swal 提示

javascript - 将点击事件绑定(bind)到我页面上的 anchor 标记

javascript - 单击输入类型=图像

javascript - 最轻的 js/css 向用户提供 SPA 正在加载/初始化的视觉反馈

javascript - 如果页面上不存在 Div 类,则显示 SweetAlert 弹出窗口

javascript - 如何在angular2中使用sweetalert2