调用iframe中的按钮点击

index.html

<html>
<head><meta http-equiv="content-type" content="text/html;charset=utf-8">
<script type="text/javascript">
    function callChild(){
            //myFrame.window.say();
            myFrame.window.document.getElementById("button").click();
             //var x= myFrame.window.document.getElementById("myen").innerHTML="kkkkk";//可以用
        }
</script></head>
<body>
<input id="button" type="button" value="调用child.html中按钮的点击" onclick="callChild()"/><br><iframe name="myFrame" 
src="child.html" width="800"></iframe>
</body>
</html>

child.html

<html>
<head><meta http-equiv="content-type" content="text/html;charset=utf-8"></head>
<script type="text/javascript">
function inenword(){
  var x=document.getElementById("enword").value;
      document.getElementById("myen").innerHTML=x;

}
</script>
<h1>查询单词</h1>
请输入英文单词:<input type="text" name="enword" id="enword">
<input type="button" id="button" value="查询" onclick="inenword()">
<br>您要查找的英文是:<span id="myen"></span>
</html>
### Vue 中嵌入 iframe调用其内部事件 在 Vue 项目中实现与 `<iframe>` 的交互主要通过 `postMessage` API 来完成跨域安全的消息传递。这种方式允许父页面向子页面发送消息以及接收来自子页面的通知。 #### 创建带有 iframe 的组件结构 定义一个简单的 Vue 组件来加载目标网站: ```vue <template> <div class="container"> <!-- 定义 iframe --> <iframe ref="myIframe" :src="url" @load="onLoad"></iframe> <!-- 操作按钮 --> <button @click="sendMessageToIFrame">Send Message to iFrame</button> </div> </template> <script> export default { data() { return { url: 'https://example.com', // 替换成实际 URL }; }, methods: { onLoad(event) { console.log('iFrame loaded'); window.addEventListener('message', this.receiveMessage); }, sendMessageToIFrame() { const message = { action: "doSomething", payload: {} }; // 获取 iframe DOM 对象并通过 contentWindow 发送消息给子页面 this.$refs.myIframe.contentWindow.postMessage(message, '*'); }, receiveMessage(event) { try { if (event.origin !== 'https://example.com') throw new Error(`Invalid origin ${event.origin}`); switch(event.data.action){ case 'response': alert(JSON.stringify(event.data.payload)); break; default: console.warn('Unknown event received from child frame.'); } } catch(error) { console.error(error.message); } } }, beforeDestroy(){ window.removeEventListener('message', this.receiveMessage); } } </script> ``` 上述代码展示了如何创建一个包含 `<iframe>` 的 Vue 组件,并设置了一个点击按钮用来触发向 iframe 内部发送消息的操作。当 iframe 加载完成后,监听器被注册以便能够接收到由 iframe 返回的信息[^2]。 为了使这段逻辑正常工作,在 iframe 页面也需要相应的处理程序来响应这些请求并将结果反馈回去。假设我们控制着 iframe 所指向的内容,则可以在该页面上添加如下 JavaScript 代码片段: ```javascript window.onmessage = function(e) { var origin = e.origin || e.originalEvent.origin; if(origin === 'https://parent-domain.com'){ // 验证来源域名 let responsePayload = {}; switch(e.data.action){ case 'doSomething':{ // 这里放置具体的业务逻辑 responsePayload = {"status": true}; break; } default:{ console.log('Unrecognized command:', e.data.action); } } parent.postMessage({action:'response',payload:responsePayload},'*'); } }; ``` 这样就可以实现在 Vue 主页和 iframe 子页面之间的双向通讯机制了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值