iframe局部刷新

通过iframe实现全局刷新和局部刷新。

原理

通过location.reload()来实现.详见代码。

代码

主页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button>全局刷新</button>
<iframe src="sub.html" frameborder="0" width="400" height="" scrolling="auto"></iframe>
<script>
window.onload = function () {
document.getElementsByTagName('button')[0].onclick = function(){
//location.reload();
//top.location.reload();
parent.location.reload();
console.log("index.html reload!");
}
}
</script>
</body>
</html>

子页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>
测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!
</p>
<button id="subtbn">局部刷新</button>
<script>
window.onload = function () {
document.getElementById('subtbn').onclick = function(){
self.location.reload();
console.log("sub.html reload!");
}
}
</script>
</body>
</html>
------本文结束 感谢阅读------