-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.html
44 lines (40 loc) · 983 Bytes
/
test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>iframe</title>
<style>
html {
height: 100%;
}
</style>
</head>
<body>
<div>
这是不同域iframe页面
<span></span><br><br>
<button onclick="transferParent()">子給父传值</button><br><br>
<button onclick="handleParentDom()">不同域操作父级的子元素</button>(跨域问题)
</div>
</body>
</html>
<script>
// 监听
window.addEventListener('message', function (event) {
console.log('子页面接受到的值', event.data.value)
})
// 给parent传值
function transferParent () {
const data = {
type: 'fromChild',
value: 'fromChild'
}
window.parent.postMessage(data, '*')
}
// 操作父级子元素
function handleParentDom () {
console.log(window.parent.document)
}
</script>