window.open 在父页面操纵子页面元素

本文介绍了一个在网页中打开新窗口并进行操作的示例代码,包括设置新窗口的位置尺寸、修改内容及触发按钮点击等操作。

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

父页面:

<!DOCTYPE html>
	<html lang="en">
	<head>
	    <meta charset="UTF-8">
	    <title>Title</title>
	    <script>
	        function showmodelwindow() {
	
	            var itop = (window.screen.availheight - 510) / 2; //获得窗口的垂直位置;
	            var ileft = (window.screen.availwidth - 650) / 2; //获得窗口的水平位置;
	            var infoWindow = window.open('testopenwindow.html', '', 'height=510px, width=650px, top=' + itop + ',left=' + ileft + ', toolbar=no,menubar=no, scrollbars=yes, resizable=no,location=no, status=no')
	
	            infoWindow.onload = function() {
	                infoWindow.document.getElementById("test").innerHTML = '2'
	                infoWindow.document.getElementById("tertcc").value="这里是你要赋的值ccccccc";
	                //表单提交
	                 //infoWindow.document.getElementById('sub_form').submit();
	                 //触发按钮
	                // infoWindow.document.getElementById("btn").onclick();
	
	             }
	        }
	
	    </script>
	</head>
	<body>
	    <input type="button" value="click it" onclick="showmodelwindow()"/>
	    <input type="button" value="c" onclick="confirmback()">
	</body>
	</html>

子页面(子页面也可以是第三方网页,如:实现在自己的页面上操纵第三方页面,实现自动填值,提交的模拟登陆,流程相似):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function to_baidu() {
            window.open('http://www.baidu.com')
        }

    </script>

</head>
<body>
    <form id="sub_form" method="post" action="http://www.bai.com">
        <input type="text" id="tertcc"></input>
         <input type="button" id="btn" onclick="to_baidu()"></input>
        <div id="test">1</div>
    </form>

</body>


</html>

效果:
这里写图片描述

表单提交效果:
这里写图片描述

触发按钮效果:
这里写图片描述

在JavaScript中,`window.open()` 方法用于打开一个新的浏览器窗口或者tab,并返回一个表示新窗口的对象,通常称为 `Window` 对象。这个对象可以让你控制新窗口的行为,包括接收来自其本身的通信。 当你想从父页面向新开的子页面传递数据,子页面需要提供一个方法来接收这个数据并可能设置一个回调。一种常见的做法是在子页面中定义一个全局变量或者函数,例如: ```javascript // 子页面(child.html) function setReturnValue(data) { window.returnValue = data; } // 或者你可以创建一个特定的对象来保存返回值 let returnValueData = null; // 如果你想将值存储在一个变量里 // 当处理完数据后,调用上面的函数或者改变全局变量 setReturnValue("这是子页面接收到的数据"); ``` 然后,在父页面通过`window.open()` 获取到子页面的`window` 对象,并尝试访问它的`returnValue`属性或者等待它触发回调: ```javascript // 父页面(parent.js) var childWin = window.open("child.html", "childFrame"); // 监听子页面的window.onload事件,当加载完成后尝试读取返回值 childWin.onload = function() { var result = childWin.returnValue; // 如果有直接赋值给window.returnValue,则可以直接获取 console.log(result); }; // 或者如果子页面设置了某个函数作为回调 childWin.childCallback = function(data) { console.log(data); }; ``` 请注意,由于同源策略的限制,子页面无法直接修改父页面的`window.returnValue`,反之亦然。这种通信方式仅适用于在同一域内的页面。如果你需要跨域交互,通常需要服务器支持或JSONP等其他技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值