Javascript 有没有办法在 IFRAME src 页面中隐藏元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27129487/
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
is there a way hide elements in IFRAME src page?
提问by user3080110
I have a very simple code. I like to hide some DIV's in my IFRAMe. So my index.html page looks like this, so the whole idea is hide the top and the leftpanel of from the Calendar.aspx page.
我有一个非常简单的代码。我喜欢在我的 IFRAMe 中隐藏一些 DIV。所以我的 index.html 页面看起来像这样,所以整个想法是隐藏 Calendar.aspx 页面的顶部和左侧面板。
any idea what's wrong with my code or how should I do this?
知道我的代码有什么问题,或者我应该怎么做?
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></<script>
<script type="text/javascript">
$( document ).ready(function() {
$("#leftpanel").hide();
$("#toppanel").hide
});
();
</script>
<iframe height="800px" width="800px" src="https://domain/Calendar/Calendar%20Color%20Overview.aspx" >
</iframe>
采纳答案by Refilon
You can try this:
你可以试试这个:
$('#iframeID').load(function(){
$('#iframeID').contents().find('#leftpanel').hide();
$('#iframeID').contents().find('#toppanel').hide();
});
Or:
或者:
$(document).ready(function(){
$('#iframeID').contents().find('#leftpanel').hide();
$('#iframeID').contents().find('#toppanel').hide();
});
This method is not possible if you want to work with different domains.
如果您想使用不同的域,则无法使用此方法。
回答by Amit Joki
The documentfocuses on the current document and not the <iframe>. You need to go through its contents and find the elements before hiding them.
将document侧重于当前文档,而不是<iframe>。content在隐藏它们之前,您需要遍历它的s 并找到元素。
$('iframe').load(function() {
$('iframe').contents().find('#leftpanel,#toppanel').hide();
});
回答by Kyle Martinez
I achieved this by creating a hidden form that posts data to the iframe src then deletes itself onpageload. Left with a iframe with src(unknown).
我通过创建一个隐藏表单来实现这一点,该表单将数据发布到 iframe src 然后在页面加载时删除自身。留下带有 src(unknown) 的 iframe。
I used post to send data to url. Need full control of target domain to receive request. Have not tried code with a GET request.
我使用 post 将数据发送到 url。需要完全控制目标域才能接收请求。尚未尝试使用 GET 请求编写代码。
//html
<div id='deleteForm'>
<form style='display: none;' class='fakeForm' id='fakeForm' target="target_iframe" action="(iframe url)" method="post">
//(optional) can send data to iframe src as a post request
<input type="hidden" name='data' value="data you may want to send"/>
<input type="submit" style='display: none;'>
</form>
</div>
<iframe id='target_iframe' src=''></iframe>
//js
window.onload = function () {
var form = document.getElementById("fakeForm");
form.submit();
var deleteParent = document.getElementById("deleteForm");
deleteParent.remove();
}
回答by rockets houston
because you have a problem in script you should remove this sign" < "in the last of this script
因为您在脚本中有问题,您应该删除此脚本最后一个中的此符号“<”
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></<script>
it should be like this
应该是这样的
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>

