js 主页面获取iframe页面元素的值.html

本文介绍了一种在主页面中获取嵌套iframe内元素值的方法,并通过示例代码展示了如何使用JavaScript在不同浏览器环境下实现这一功能。适用于前端开发者解决跨域iframe通信问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>

<html lang="zh-CN">

  <head>

    <meta charset="UTF-8" />

    <title>02.主页面获取iframe页面元素的值</title>    

    <script>

      /*参考:https://blog.csdn.net/qq_27626333/article/details/77990196*/

      function getValue() {

        /* 获取 iframe 的内嵌文档的输入框的值,并跳转到某链接。*/

        let tmp = ''

        // 在IE下。

        if (document.frames) {

          tmp +=

            document.frames['myFrame'].document.getElementById('test').value

        }

        // 其他浏览器。

        else {

          // dom 方法。

          // let iframe = document.getElementById('myFrame');

          // tmp = iframe.contentWindow.document.getElementById('test').value;

          // jquery 方法。

          tmp = document.querySelector('#myFrame').contentWindow.document.querySelector('#test').value

          // console.log("tmp.split(':'):", tmp.split(':'));

          // (2) ["欢迎访问", "http://blog.csdn.net/qq_27626333"]

        }

        // console.log("location:", location);

        if (confirm(tmp)) {

          // 单击确定后的动作。

          location.href = tmp.split(':')[1]

        }

      }

    </script>

  </head>

  <body onload="getValue()">

    <iframe id="myFrame" src="03.nestedWeb.html" width="600px" height="100px;"></iframe>

  </body>

</html>

<!DOCTYPE html>

<html lang="zh-CN">

  <head>

    <meta charset="UTF-8" />

    <title>03.nestedWeb</title>

    <style>

      #test {

        border: solid royalblue 2px;

        border-radius: 4px;

        width: 500px;

        color: #797979;

        height: 50px;

        line-height: 50px;

        font-size: 20px;

      }

    </style>

  </head>

  <body>

    <input

      type="text"

      id="test"

      value="欢迎访问:https://blog.csdn.net/qq_27626333/article/details/77990196"

    />

  </body>

</html>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值