从iframe中的其他页面延长iframe本身 [英] Lengthening an iframe from another page within the iframe itself

查看:89
本文介绍了从iframe中的其他页面延长iframe本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为学校建立一个网站,而且我很缺乏经验。

我遇到的问题是:



我有一个主页上的iframe,我想根据点击的链接制作更长的时间。

这个部分很简单,但是当点击链接时,



因此,我有一个外部脚本,我在这里执行

 <$ (document.getElementById)
{
this.obj = document.getElementById(name); code> function getObj(name)
{
if
this.style = document.getElementById(name).style;
}
else if(document.all)
{
this.obj = document.all [name];
this.style = document.all [name] .style;
}
else if(document.layers)
{
this.obj = document.layers [name];
this.style = document.layers [name];
}
}

var frame,div1;
function mainInit(){
frame = new getObj(idFrame);


函数init2(){
div1 = new getObj(divWithTable);
div1.style.visibility =hidden;
}

函数ShowDiv(){
div1.style.visibility =visible;
//frame.obj.height = 1000;
}

所以我有一个< body onload =我的主页上的mainInit()> 和iframe中页面上的< body onload =init2()> 它也有一个按钮,其中包含 onclick =ShowDiv()



现在的问题是:

当我点击一个在其中的页面上显示div的按钮时,我无法更改iframe的长度。我需要以某种方式从第一页返回已定义的iframe,因此我可以在第二页中使用它。

是在同一个来源,你应该能够访问链接并附加一个功能。 :)

  //找到iframe(在这种情况下,通过它的标签< iframe>)
var iframe = document.getElementsByTagName( 'IFRAME')[0];
//获取iframe的内容
// ||意思是说,这是为了说明不同浏览器允许访问iframe内容的方式
//如果你不能获取contentDocument,那么它会给你contentWindow.document
var iframeContent = iframe.contentDocument || iframe.contentWindow.document;

//最后我们从iframe获得链接元素
//在这个例子中,我们所有的链接都有'my-link'类,但是可以通过标签名称<一个> (以同样的方式获得iframe)
var iframeLinks = iframeContent.getElementsByClassName('my-link');

//然后将您的监听器添加到链接

如果您正在运行这在本地你将需要一个虚拟服务器。有很多很容易运行。 :)

I am currently making a website for school and I'm quite inexperienced.
The problem I've encountered is:

I have an iframe on a homepage, which I want to make longer depending on the link clicked.
That part is easy, what is not however is making the iframe longer when clicking on a link within the iframe.

So I have an external script in which I do

function getObj(name)
{
  if (document.getElementById)
  {
    this.obj = document.getElementById(name);
    this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
    this.obj = document.all[name];
    this.style = document.all[name].style;
  }
  else if (document.layers)
  {
    this.obj = document.layers[name];
    this.style = document.layers[name];
  }
}

var frame, div1;  
function mainInit(){
  frame = new getObj("idFrame");
}

function init2(){
     div1 = new getObj("divWithTable");
     div1.style.visibility = "hidden";
}

function ShowDiv(){
  div1.style.visibility = "visible";
  //frame.obj.height = 1000;
}

So I have a <body onload="mainInit()"> on my homepage and a <body onload="init2()"> on the page within the iframe, which also has a button with an onclick="ShowDiv()".

What the problem is now is:
I can not change the length of the iframe when I click a button that shows a div on the page within it. I would need to somehow return the defined iframe from the first page, so I can use it on the second.

解决方案

Provided you are on the same origin, you should be able to access the links and attach a function to them. :)

// Find the iframe (in this case by it's tag <iframe>)
var iframe = document.getElementsByTagName('iframe')[0];
// Get the content of the iframe
// The || means or, this is to account for the way different browser allow access to iframe content
// If you cant get the contentDocument, then it'll give you the contentWindow.document 
var iframeContent = iframe.contentDocument || iframe.contentWindow.document;

// Finally we get the link elements from the iframe
// In this example all of our links have the class 'my-link', but you could get by tag name <a> (in the same way we got the iframe)
var iframeLinks = iframeContent.getElementsByClassName('my-link');

// Then add your listeners to the links

If you are running this locally you will need a virtual server. There are lots out there that are really easy to run. :)

这篇关于从iframe中的其他页面延长iframe本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆