单击一次后如何让iframe消失?

时间:2014-01-17 15:16:45

标签: html iframe click

正如标题所说,我还没有真正开始创建代码,因为我需要一些帮助。我不擅长javascript或jquery脚本,我刚开始学习html所以我只知道基础知识。现在,回到主题上。

我想在点击后立即取消iframe,但正如我所说,我刚刚开始编写脚本。任何人都有任何想法?

3 个答案:

答案 0 :(得分:1)

以下是使用普通旧JavaScript执行此操作的方法。请注意,单击iframe中加载的页面可能不会调用您的事件处理程序,这就是我为此示例添加边框的原因(单击边框将执行事件处理程序)。您可能需要将iframe与其他元素重叠,并在重叠元素上捕获click事件。

<iframe src="http://someurl" onclick="this.style.display = 'none'" style='border: solid 10px red'></iframe>

答案 1 :(得分:0)

编辑:根据您的评论。

要包含jQuery,请将以下内容放入HTML <head></head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

然后使用this w3schools article了解如何将JavaScript附加到HTML。

在你的Javascript中,你可以像这样使用jQuery:

// Run all of the following code when our document is loaded
$( document ).ready(function() {

    // Setup an event handler. Says, when we click on an iframe, run this function
    $("iframe").on("click",function(){

      $(this).remove();//jQuery function to completely remove from DOM
      /* OR */
      $(this).css("display","none"); //jQuery function that completely hides in CSS

    };


});

既然你说你是编程HTML的新手,你就会想要阅读和练习JS。 Here's介绍JS和jQuery。

答案 2 :(得分:0)

您可以使用CSS执行此操作,例如将iframe设为id,例如将其命名为“iframe_id”: - #iframe_id.click{ display:none;}