[英]How to change iframe src from another page
I have a php page that includes another php page named 'tree.php' and also contain an iframe .The Iframe calls the page 'contentpage.php'.Then i want to change the src of iframe using javascript or jquery from tree.php.I have alredy tried these 我有一个php页面,其中包含另一个名为'tree.php'的php页面,还包含一个iframe .Itrame调用页面'contentpage.php'。然后我想用tree.php中的javascript或jquery更改iframe的src我已经尝试过这些了
document.getElementById['contentframe'].src = "contentpage.php";
$("#contentframe").attr("src", "contentpage.php");
Assuming all the frames are in the same domain, this can be done easily. 假设所有帧都在同一个域中,这可以很容易地完成。
<html>
<head>
<title>Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
var change_iframe_src = function(new_src) {
$("#myframe-content").attr('src', new_src);
}
</script>
</head>
<body>
<!-- frame which includes your tree.php -->
<iframe src="iframe.html" width="200" height="200" border="1"></iframe>
<!-- frame for which we will change src attribute -->
<iframe id="myframe-content" src="" width="400" height="200" border="1"></iframe>
</body>
Now in tree.php attach a click handler for something that should change the src of the iframe attribute (possibly some link). 现在tree.php附加一个单击处理的东西 ,要改变iframe的属性(可能还有一些链接)的src。 Example:
例:
<html>
<head>
<title>Tree</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#nav a").click(function(e) {
e.preventDefault();
// call function in a parent frame
parent.window.change_iframe_src(this.rel);
})
})
</script>
</head>
<body>
<div id="nav">
<a href="#" rel="iframe2.html">Change iframe src to iframe2.html</a>
<a href="#" rel="iframe3.html">Change iframe src to iframe3.html</a>
</div>
</body>
Now with this kind of setup the links in the tree.php would call the function defined in parent window (the window which embeded the tree.php itself). 现在有了这种设置,tree.php中的链接将调用父窗口中定义的函数(嵌入tree.php本身的窗口)。 That function (change_iframe_src in this example) takes one argument which is a new url to which src attribute of the frame myframe-content should be changed to.
该函数(本例中为change_iframe_src)接受一个参数,该参数是myframe-content的 src属性应更改为的新url。
Again, this works as long as the base document and the iframe with tree.php are documents in the same domain. 同样,只要基础文档和带有tree.php的iframe是同一域中的文档,这就可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.