gpt4 book ai didi

html - 如何创建指向另一个页面的链接并为该页面上的 iframe 指定另一个 URL?

转载 作者:行者123 更新时间:2023-11-28 01:18:25 24 4
gpt4 key购买 nike

我有一个名为 dash.php 的页面,其中有一个 iframe (id="iframe")。在第三页我有链接:

<a href="dash.php">Add</a>
<a href="dash.php">Edit</a>
  • 单击“添加”链接后,我想切换到 dash.php 页面并让它在 iframe 中加载特定页面。
  • 单击“编辑”链接后,我想切换到 dash.php 页面并让它在 iframe 中加载一个不同的特定页面。

我对如何执行此操作一无所知,我希望能向正确的方向插入。谢谢。

最佳答案

在您的 URL 中添加一个额外的参数应该可以解决这个问题:

<强>1。主页面index.php

添加 -> href="dash.php?content=add"

编辑 -> href="dash.php?content=edit"

<强>2。 dash.php

2.1. 抓取内容值:

$content = $_GET['content'];

2.2.测试它:

if($content =='add')
$myIFrame = 'http://www.stackoverflow.com';
else
$myIFrame = 'http://www.w3schools.com';

2.3. 通过设置其来源来显示您的 iframe:

src="<?= $myIFrame ?>"

如您所见,这里不需要 Javascript。

**

编辑:

**

但是,如果您需要一个工作的 JS,这里是,使用相同的 main.php 文件:

var iframe;
iframe = document.getElementsByTagName('iframe')[0];

function $_GET(param) {
var vars = {};
window.location.href.replace(
/[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
function( m, key, value ) { // callback
vars[key] = value !== undefined ? value : '';
}
);

if ( param ) {
return vars[param] ? vars[param] : null;
}
return vars;
}

if($_GET('content') == 'add')
iframe.src="http://www.stackoverflow.com";
else
iframe.src="http://www.w3schools.com";

关于html - 如何创建指向另一个页面的链接并为该页面上的 iframe 指定另一个 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34794759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com