html中如何获取父页面的值

在HTML中,要获取父页面的值,可以使用JavaScript的
window.parent对象,以下是一个简单的示例:,1、创建一个HTML文件,例如
index.html,并在其中添加以下内容:,2、创建一个JavaScript文件,例如
script.js,并在其中添加以下内容:,3、在父页面(例如
parent.html)中添加以下内容:,在这个示例中,我们创建了两个HTML文件:
index.html
parent.html
index.html是子页面,
parent.html是父页面,我们在子页面中添加了一个按钮,当点击该按钮时,会调用
getParentValue()函数,这个函数会获取父页面的值,并显示在一个弹出框中。,
,<!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF8″> <meta name=”viewport” content=”width=devicewidth, initialscale=1.0″> <title>获取父页面值示例</title> </head> <body> <h1>子页面</h1> <button onclick=”getParentValue()”>获取父页面值</button> <script src=”script.js”></script> </body> </html>,function getParentValue() { // 获取父页面的iframe元素 var parentIframe = window.parent.document.getElementById(‘parentIframe’); // 获取父页面的值 var parentValue = parentIframe.contentWindow.document.getElementById(‘parentValue’).value; // 显示父页面的值 alert(‘父页面的值是:’ + parentValue); },<!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF8″> <meta name=”viewport” content=”width=devicewidth, initialscale=1.0″> <title>父页面</title> </head> <body> <h1>父页面</h1> <iframe id=”parentIframe” srcdoc=”<p><input type=’text’ id=’parentValue’ value=’这是父页面的值’></p>”></iframe> </body> </html>,

版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《html中如何获取父页面的值》
文章链接:https://zhuji.vsping.com/428216.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。