IFrame编辑

关于IFrame编辑模式

开启编辑模式

document.getElementById("iframeid").contentWindow.document.designMode = "on";
document.getElementById("iframeid").contentWindow.document.contentEditable = true;

注意:如果是使用外链时需要处理同源校验,并且需要在控件内容加载完成后,设置编辑模式,如果不是链接,不需要监听加载完成,可以直接设置编辑模式。

默认函数

document.getElementById("iframeid").contentWindow.document.execCommand(cmd, isDefaultShowUI, value);

注意:一般的编辑操作JS已经提供了函数库,参考以下函数对照表即可。

函数对照表

命令(cmd) 展示界面交互(isDefaultShowUI) 参数值(value)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现: 1. 在父页面中添加一个 iframe 标签,设置其 src 属性为要嵌入的页面的 URL。 2. 在 iframe 中的页面中添加需要监听的事件。 3. 在父页面中获取 iframe 中的输入框元素,以及其他需要监听的元素。 4. 使用 JavaScript 中的 postMessage() 方法向 iframe 中发送消息,监听 iframe 中的事件并做出相应处理。 下面是一个示例代码: 父页面代码: ```html <!DOCTYPE html> <html> <head> <title>父页面</title> </head> <body> <iframe id="my-iframe" src="inner-page.html"></iframe> <script> // 获取 iframe 中的输入框元素 var iframe = document.getElementById("my-iframe"); var innerInput = iframe.contentWindow.document.getElementById("inner-input"); // 监听 iframe 中的事件 window.addEventListener("message", function(event) { if (event.origin !== "http://localhost:8000") { return; } // 处理事件 if (event.data.type === "input") { console.log("输入框的值为:" + event.data.value); } else if (event.data.type === "button") { console.log("点击了按钮!"); } }); // 向 iframe 发送消息 function sendMessage(type, value) { iframe.contentWindow.postMessage({ type: type, value: value }, "*"); } </script> </body> </html> ``` 内部页面代码: ```html <!DOCTYPE html> <html> <head> <title>内部页面</title> </head> <body> <input type="text" id="inner-input"> <button id="inner-button">点击我</button> <script> // 监听输入框的输入事件 var innerInput = document.getElementById("inner-input"); innerInput.addEventListener("input", function() { // 向父页面发送消息 window.parent.postMessage({ type: "input", value: innerInput.value }, "*"); }); // 监听按钮的点击事件 var innerButton = document.getElementById("inner-button"); innerButton.addEventListener("click", function() { // 向父页面发送消息 window.parent.postMessage({ type: "button" }, "*"); }); </script> </body> </html> ``` 在这个示例中,父页面和内部页面都是同一个域名下的页面,所以使用了通配符 "*" 来接受所有来源的消息。在实际应用中,应该根据需要设置特定的来源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值