Typo:
事件被调用
message
不是
iframe_message
:
const frame_content = `<html>
<head>
</head>
<body>
<p id="editor" contentEditable="true">edit me</p>
</body>
<script>
document.getElementById("editor").addEventListener("input", function() {
console.log("input event fired");
window.parent.postMessage('iframe_message', '*')
}, false);
<\/script>
</html>`;
frame.src = 'data:text/html,' + encodeURIComponent(frame_content);
window.addEventListener("message", function() {
// ^-- Here it is "message"
console.log('iframe message')
});
<iframe id="frame"></iframe>