简体   繁体   English

点击删除div或iframe

[英]remove div or iframe on click

There is a iframe fb group like, and need to hide it on click, but jQuery doesnt work 有一个iframe fb组,需要在单击时将其隐藏,但jQuery无法正常工作

<script>
    $(".closeFrame").click(function(){
        $("#test").remove();
    });
</script>


<div id = "test" class='like-btn'>
    <iframe = "#"  class="closeFrame" src="https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fzhelechka&width=51&layout=button&action=like&show_faces=false&share=false&height=65&appId" width="51" height="65" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" ></iframe>
</div>

First of all, you need to understand the difference between the two elements: the button is the one that has to be clicked and the other element (the div or the iframe) is the one that is going to be hidden. 首先,您需要了解两个元素之间的区别:按钮是必须单击的按钮,另一个元素(div或iframe)是将要隐藏的按钮。 It is important to understand this correctly in order to code the script properly. 为了正确地编写脚本,正确理解这一点很重要。

Javascript code (using jQuery) Javascript代码(使用jQuery)

$(".hide").click(function () {
    $(".elementtohide").hide();
});

See it in action: JSFiddle 实际操作中看到它: JSFiddle

Explanation 说明

The idea is to attach an event to the button that has to be clicked in order to receive the user's click (this is made with the jQuery method called .click() ). 这个想法是将一个事件附加到必须单击的按钮上,以便接收用户的点击(这是通过名为.click()的jQuery方法完成的)。 Inside that, we must especify what is going to happen when the click is done, so we make a call to the jQuery method called .hide() in order to hide the element (I have used a div but you can use an iframe or whatever you want). 在其中,我们必须指定点击完成后会发生的情况,因此我们调用了名为.hide()的jQuery方法以隐藏元素(我使用了div,但可以使用iframe或无论你想要什么)。

Note : as mentioned in comments, .hide() and .remove() have different behaviours: the first one just adds the style display: none to the element and the second removes the element from the DOM, which means that if you had to show it again, you would have to create it. 注意 :如注释中所述, .hide().remove()具有不同的行为:第一个只是添加样式display: none到元素,第二个从DOM中删除了元素,这意味着如果必须再次显示,则必须创建它。 Use the one that better fits your needs. 使用更适合您需求的那一种。

A code like this can work 这样的代码可以工作

<div id = "test" class='like-btn'>
    <iframe = "#" onclick="this.parentNode.style.display = 'none'" class="closeFrame" src="https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fzhelechka&width=51&layout=button&action=like&show_faces=false&share=false&height=65&appId" width="51" height="65" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" ></iframe>
</div>

but When you click into an iframe you will click on another part of the world so will not work so the code above will work only if the element isn't an iframe(if is a DIV can work) 但是当您点击iframe时,您将点击世界的另一部分,因此将无法正常工作,因此上述代码仅在元素不是iframe的情况下才有效(如果DIV可以工作)

so the solution should be 所以解决方案应该是

<div id = "test" class='like-btn'>
<button onclick="document.getElementById('test').style.display = 'none'" value="Press me" name="closeBtn"/>
    <iframe = "#" onclick="this.parentNode.style.display = 'none'" class="closeFrame" src="https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fzhelechka&width=51&layout=button&action=like&show_faces=false&share=false&height=65&appId" width="51" height="65" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" ></iframe>
</div>

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

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