javascript iframe 只在点击后显示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15480936/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:56:11  来源:igfitidea点击:

iframe show only after click?

javascriptajaxhtmliframe

提问by Thomas Mccaffery

I have the following code: Iframe is being shown but I like to hide it until form is submitted. is it better maybe to use Div instead of iframe? I like to get get rid of div thats already there when form is submitted.

我有以下代码: Iframe 正在显示,但我喜欢在提交表单之前隐藏它。使用 Div 而不是 iframe 更好吗?我喜欢摆脱提交表单时已经存在的 div。

<script type="text/javascript">  
function showIFrame() {  
 var iframe = document.getElementById("myiframe");  
 iframe.style.display="block";  
}  
</script>

in my css file

在我的 css 文件中

#myiframe {
display:none; }

<form name="search" id="search" method="get"      action="http://tmccaffery.outsideagents.com/travel/car/search.rvlx" target="carsearch">

<iframe id="myframe" name="carsearch" width="535" height="625" scrolling="no"   frameborder="0"></iframe>


<button type="submit" id="SearchCarButton" class="submitBtn" onclick="showIFrame()"/>  <span>Search</span></button>

回答by James Donnelly

You're using id="myframe"in the HTML and #myiframein the CSS.

id="myframe"在 HTML 和#myiframeCSS 中使用。

Either change id="myframe"to id="myiframe"or change #myiframeto #myframe.

更改id="myframe"id="myiframe"或更改#myiframe#myframe

回答by adi

You're using the wrong id - in your css you're using "#myiframe" instead of "#myframe" - no "i".

您使用了错误的 id - 在您的 css 中,您使用的是“#myiframe”而不是“#myframe”——没有“i”。

Here's the correct code:

这是正确的代码:

#myframe { display:none; }

回答by Gosre

iframe.style.visibility="hidden";

and to show it:

并展示它:

iframe.style.visibility="visible";

Edit: And you can use the onsubmit="JavaScriptCode" event to trigger the change.

编辑:您可以使用 onsubmit="JavaScriptCode" 事件来触发更改。

Reference: http://www.w3schools.com/jsref/event_form_onsubmit.asp

参考:http: //www.w3schools.com/jsref/event_form_onsubmit.asp

回答by Mattios550

Change your script to this :)

将您的脚本更改为此:)

<script type="text/javascript">  
function showIFrame() {  
var iframe = document.getElementById("myiframe");  
iframe.style.visibility="visible";
}  
</script>

回答by jcubic

jQuery have load event that is executed when iframe is loaded:

jQuery 具有加载 iframe 时执行的加载事件:

$('#myframe').load(function() {
   $(this).show();
});