iframe改变自己的src [英] iframe change own src

查看:551
本文介绍了iframe改变自己的src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i有一个iframe显示一个网页。

是否可以在按钮上更改iframe src单击iframe中显示的页面?



ami



i have an iframe displaying a web page.
is it possible to change the iframe src when a button on the page displayed in the iframe is clicked?

ami

推荐答案

是的,你可以。这里的关键是将iframe视为它自己的浏览器窗口/标签。



考虑下面的html摘录 - 显示在内部的代码iframe。



-iFrameSrc.html ---------------------------- -----------------------------

Javascript:

Yes, you can. The key here is to just treat the Iframe like it's a browser window/tab in it's own right.

Consider the following excerpt of html - code that is displayed inside the iframe.

-iFrameSrc.html---------------------------------------------------------
Javascript:
function changeSrc()
{
    var oiFrame = document.getElementById('mIframe');
    oiFrame.src = 'index.php';
}

function changeSrc2()
{
    window.location = 'index.php';
}





HTML:



HTML:

<body>
    Contents of Iframe
    <button onclick='changeSrc2();'>Change iFrame src</button>
</body>





-whatever html的------------------------------------------------ ------------------



现在,请考虑以下摘录 - 包含iframe的代码

HTML:



-whatever.html------------------------------------------------------------------

Now, consider the following excerpt - code that contains the iframe
HTML:

<iframe id='mIframe' src='iFrameSrc.html'/>







第一个函数 changeSrc 在那里什么都不做 - 第一行代码返回null。你显然不能设置null的src属性。



第二个函数将改变在whatever.html中找到的iframe的src属性。您会注意到,如果您只是在浏览器地址栏中输入iframeSrc.html,您将在整个窗口中显示该页面。单击该按钮将更改显示的地址_和_显示的内容。关键是,更改网址的代码是相同的,无论页面是按原样显示还是页面都显示在iframe中。





但是,我们仍然可以使 changeSrc 函数工作。但我们必须将它从iFrameSrc.html中删除并粘贴到whatever.html中。



您可以更改iframe的src - 来自iframe和来自iframe在它之外。在每种情况下,你只需要采用不同的方式。 changeSrc 将从外部执行此操作。 changeSrc2 将从内部完成。




The first function, changeSrc will do nothing there - the first line of code returns null. You obviously can't set the src attribute of null.

The second function will change the src attribute of the iframe found in whatever.html. You'll notice that if you just type iframeSrc.html into the browsers address-bar, you'll get the page in the whole window. Clicking the button will change the displayed address _and_ displayed content. The point is, the code for changing the url is the same, whether the page is displayed as is or if the page is shown inside an iframe.


However, we can still make the changeSrc function work. But we have to remove it from iFrameSrc.html and paste it into whatever.html.

You can change the src of an iframe - both from within the iframe and from outside of it. You just have to go about it differently in each case. changeSrc will do so from outside. changeSrc2 will do it from inside.


当然。只需使用这样的JavaScript:



Sure. Just use JavaScript like this:

var oiFrame = document.getElementById(iFrame);
oiFrame.src = someurl;


这篇关于iframe改变自己的src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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