Javascript 默认隐藏 iframe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4810247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hiding an Iframe by Default
提问by Reti
In one of the pages I need to have an iframe, but when the user loads the page I want it to be hidden by default. Right now I have a hide and show button to hide and show the iframe, but it is shown by default. How can I hide it by default?
在其中一个页面中,我需要一个 iframe,但是当用户加载页面时,我希望它默认隐藏。现在我有一个隐藏和显示按钮来隐藏和显示 iframe,但它默认显示。默认情况下如何隐藏它?
Here's the code I have for the two buttons
这是我的两个按钮的代码
<input type="button" value="Show Graph" onClick="$('graph').show();">
<input type="button" value="Hide Graph" onClick="$('graph').hide();">
回答by karlcow
Did you try to use CSS like this?
你试过像这样使用 CSS 吗?
iframe {
display:none;}
Though that depends if you want to have the area reserved too. With a sketch it would be easier to know.
虽然这取决于您是否也想保留该区域。有了草图就更容易知道了。
You can hide on load with jQuery, too.
您也可以使用 jQuery 在加载时隐藏。
function(){$('graph').hide();}
回答by Rion Williams
Also - since you are already using jQuery you could just simply hide the iFrame on the page load like so:
另外 - 由于您已经在使用 jQuery,您可以简单地在页面加载时隐藏 iFrame,如下所示:
<script type="text/javascript">
$(document).ready(function()
{
$('#graph').hide();
});
</script>
Working demo :Demo
工作演示:演示
So you will need to make sure of the following changes:
因此,您需要确保以下更改:
Iframe :
框架:
Add id="graph"
to your iframe.
添加id="graph"
到您的 iframe。
onClick Events:
点击事件:
Add onClick="$('#graph').show();"
and onClick="$('#graph').hide();"
respectively
添加onClick="$('#graph').show();"
和onClick="$('#graph').hide();"
分别
回答by James Long
If you put this in your <head>
tag, it should hide it for you
如果你把它放在你的<head>
标签中,它应该为你隐藏它
<script type="text/javascript">
$(function() {
$('graph').hide();
});
</script>
Although you might was to double-check that you're just using 'graph'
- in jQuery that would refer to a <graph>
element; '#graph'
would refer to the iframe with the id "graph"
尽管您可能要仔细检查您是否正在使用'graph'
- 在 jQuery 中会引用一个<graph>
元素;'#graph'
将引用 ID 为“graph”的 iframe
回答by codingpuss
There are three ways I know:
我知道的三种方式:
You set the height and width to 0.
Set an ID for the iframe (for example "hideframe") and in the head or in external stylesheet add this
#hideframe{display: none;}
Use jQuery
$(document).ready(function(){ $("#hideframe").css("display", "none"); });
Or
$(document).ready(function(){ $("#hideframe").hide(); });
您将高度和宽度设置为 0。
为 iframe 设置一个 ID(例如“hideframe”)并在头部或外部样式表中添加这个
#hideframe{display: none;}
使用 jQuery
$(document).ready(function(){ $("#hideframe").css("display", "none"); });
或者
$(document).ready(function(){ $("#hideframe").hide(); });
Make sure jQuery is actually included in the head of your page. Many times I hear people say something doesn't work in jQuery. It turns out their URLs to the jQuery library were wrong so make sure you don't fall into this situation.
确保 jQuery 确实包含在页面的头部。很多时候我听到人们说某些东西在 jQuery 中不起作用。事实证明,他们指向 jQuery 库的 URL 是错误的,因此请确保您不会陷入这种情况。
回答by zack999
Am I missing someting.. but you can make a self invoking a jQuery function as follows
我是不是遗漏了什么……但是你可以像下面这样自调用一个 jQuery 函数
(function (){
$('iframe').hide();
})();
(function (){
$('iframe').hide();
})();