Javascript 单击删除div或iframe
有一个类似iframe fb的组,需要在单击时隐藏它,但jQuery不起作用Javascript 单击删除div或iframe,javascript,jquery,html,css,Javascript,Jquery,Html,Css,有一个类似iframe fb的组,需要在单击时隐藏它,但jQuery不起作用 <script> $(".closeFrame").click(function(){ $("#test").remove(); }); </script> <div id = "test" class='like-btn'> <iframe = "#" class="closeFrame" src="https://www.fac
<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>
$(“.closeFrame”)。单击(函数(){
$(“#测试”).remove();
});
首先,您需要了解这两个元素之间的区别:按钮是必须单击的元素,而另一个元素(div或iframe)是要隐藏的元素。正确理解这一点对于正确编写脚本非常重要
Javascript代码(使用jQuery)
在行动中看到它:
解释
其思想是将事件附加到必须单击的按钮上,以便接收用户的单击(这是通过调用jQuery方法实现的)。在这里面,我们必须详细说明单击完成后将发生什么,因此我们调用调用jQuery方法以隐藏元素(我使用了div,但您可以使用iframe或任何您想要的东西)
注意:如注释中所述,它们有不同的行为:第一个只是将样式
display:none
添加到元素中,第二个从DOM中删除元素,这意味着如果必须再次显示它,则必须创建它。使用更适合您需要的元素。首先,您需要了解这两个元素之间的区别:按钮是必须单击的元素,而另一个元素(div或iframe)是要隐藏的元素。正确理解这一点对于正确编写脚本非常重要
Javascript代码(使用jQuery)
在行动中看到它:
解释
其思想是将事件附加到必须单击的按钮上,以便接收用户的单击(这是通过调用jQuery方法实现的)。在这里面,我们必须详细说明单击完成后将发生什么,因此我们调用调用jQuery方法以隐藏元素(我使用了div,但您可以使用iframe或任何您想要的东西)
注意:如注释中所述,它们有不同的行为:第一个只是将样式
display:none
添加到元素中,第二个从DOM中删除元素,这意味着如果必须再次显示它,则必须创建它。使用更适合您需要的代码。这样的代码可以工作
<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>
但是
当您点击一个iframe时,您将点击世界的另一部分,因此无法工作,因此只有当元素不是iframe(如果是DIV,则可以工作)时,上面的代码才能工作
so
解决办法应该是
<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>
像这样的代码可以工作
<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>
但是
当您点击一个iframe时,您将点击世界的另一部分,因此无法工作,因此只有当元素不是iframe(如果是DIV,则可以工作)时,上面的代码才能工作
so
解决办法应该是
<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>
取出并使用.hide();基思,请解释一下。remove()
方法具有不同的行为,可能更可取..hide();不起作用IFrame中的=“#”
是什么?取出并使用.hide()的可能副本;基思,请解释一下。remove()
方法具有不同的行为,可能更可取..hide();不起作用在IFrame中的=“#”
是什么可能的重复