将iframe中的iframe作为目标父级div [英] Target parent div containing iframe from iframe
问题描述
< div class =iframe-holder>
< span>< / span>
< iframe src =iframe.htmlwidth =200height =200
< / div>
< div class =iframe-holder>
< span>< / span>
< iframe src =iframe.htmlwidth =200height =200
< / div>
< div class =iframe-holder>
< span>< / span>
< iframe src =iframe.htmlwidth =200height =200
< / div>
< script>
函数where_am_i(iframe_parent_div,msg){
$(iframe_parent_div).find('span')。html(msg);
}
< / script>
然后在我的iframe-holder.html中我基本上有这个
< a href =#id =msg>在Parent Div中显示讯息< / a>
< script>
//我想基本上包含
$('msg')。click(function(event){
event.preventDefault();
var iframe_parent_div = ???;
parent.where_am_i(iframe_parent_div,'this is this where iframe is');
});
< / script>
所以基本上我的问题是我该如何定位父div(div.iframe-holder) iframe实例我点击显示消息按钮?这甚至有可能吗?
我知道我可以通过源代码发送一个变量,其中iframe.html?index = 0代表第一个iframe.html?index = 1等等,但这对我来说不是一个可行的解决方案,因为在iframe中会有很多导航到不同的页面,所以传递到所有页面的索引不是一个选项。
var iframe_parent_div = window.frameElement?解决方案 window.frameElement.parentNode:null;
So basically I have something like this
<div class="iframe-holder">
<span></span>
<iframe src="iframe.html" width="200" height="200"
</div>
<div class="iframe-holder">
<span></span>
<iframe src="iframe.html" width="200" height="200"
</div>
<div class="iframe-holder">
<span></span>
<iframe src="iframe.html" width="200" height="200"
</div>
<script>
function where_am_i(iframe_parent_div,msg){
$(iframe_parent_div).find('span').html(msg);
}
</script>
and then in my iframe-holder.html I basically have this
<a href="#" id="msg">Show Message In Parent Div</a>
<script>
//i want to basically imp
$('msg').click(function(event){
event.preventDefault();
var iframe_parent_div=???;//how do I get this?
parent.where_am_i(iframe_parent_div,'This is where this iframe is');
});
</script>
So basically my question is how can I target the parent div (div.iframe-holder) of the iframe instance I am clicking the "Show Message" button from? Is this even possible?
I know that I can send through the source a variable with the index i.e. iframe.html?index=0 for the first iframe.html?index=1 for the second and so on but this is not a viable solution for me because in the iframe there is going to be a lot of navigating to different pages so passing around to all the pages the index is not an option.
Do:
var iframe_parent_div = window.frameElement ? window.frameElement.parentNode : null;
这篇关于将iframe中的iframe作为目标父级div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!