简体   繁体   English

尝试在元素隐藏时删除iframe源

[英]Trying to remove iframe source when element is hidden Jquery

I am trying to remove an iframe source when the element is hidden 元素隐藏时,我正在尝试删除iframe源

  $(document).ready(function() {
    $("#myModal").is(":hidden",function(){
        $('iframe').attr("src","");
    });

    $("#myModal").is(":visible",function(){
            $('iframe').attr("src","http://google.com");
        });
   });

by default, the modal i created has a 默认情况下,我创建的模态有一个

style="display:none"

what I would like to happen is to have the iframe source empty if the modal is hidden 我想发生的是,如果模式被隐藏,则将iframe源为空

<iframe src="">

and load a source when the modal is visible or if 并在模态可见或以下情况时加载源

style="display:block;"

I tried several method but nothing works and came up with the one above but still doesn't work. 我尝试了几种方法,但是没有任何效果,想出了上面的一种方法,但是仍然行不通。

If you are using Bootstrap modal, you could refer this: Modal#Events 如果使用的是Bootstrap模态,则可以参考以下内容: Modal#Events

Then, your js code can be edited to: 然后,您的js代码可以编辑为:

$(document).ready(function() {
    $("#myModal").on("hidden.bs.modal",function(){
        $('iframe').attr("src","");
    });

    $("#myModal").on("shown.bs.modal",function(){
        $('iframe').attr("src","http://google.com");
    });
});

To make the code clearly, you could write: 为了使代码清晰可见,您可以编写:

$(document).ready(function() {
    var modal = $("#myModal"), iframe = $('iframe');

    var onhidden = function(){
        iframe.attr("src","");
    };

    var onshown = function(){
        iframe.attr("src","http://google.com");
    };

    modal.on("hidden.bs.modal", onhidden)
         .on("shown.bs.modal", onshown);
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM