iframe 实时预览 textarea 中的内容

代码(直接本地保存为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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!DOCTYPE HTML>
<html>

<head>
<title> test </title>
</head>

<body>
<style>
@media screen and (max-width: 480px) {
img {
max-width: 100%;
}
}
</style>
<div id="app">
<textarea name="htmlContent" id="htmlContent" cols="80" rows="10">

<style>
html {
height: 100vh;
}
body {
height: inherit;
background: #2e576b;
display: -ms-grid;
display: grid;
}
.container {
margin: auto;
}
.card {
position: relative;
width: 300px; height: 350px;
background: #fff;
border-radius: 2px;
box-shadow: 0 2px 15px 3px rgba(0, 0, 0, 0.08);
overflow: hidden;
}
.card::after {
content: '';
display: block;
width: 100%; height: 100%;
background: linear-gradient(to bottom, #0065a8, rgba(221, 238, 255, 0.4) 46%, rgba(255, 255, 255, 0.5));
}
.wave {
position: absolute;
top: 3%; left: 50%;
width: 400px; height: 400px;
margin-top: -200px; margin-left: -200px;
background: #0af;
border-radius: 40%;
opacity: .4;
animation: shift 3s infinite linear;
}
.wave.w2 {
background: yellow;
opacity: .1;
animation: shift 7s infinite linear;
}
.wave.w3 {
animation: shift 5s infinite linear;
background: crimson;
opacity: 0.1;
}
@-webkit-keyframes shift {
from {
transform: rotate(360deg);
}
}
@keyframes shift {
from {
transform: rotate(360deg);
}
}
</style>

<div class="container">
<div class="card">
<div class="wave w1"></div>
<div class="wave w2"></div>
<div class="wave w3"></div>
</div>
</div>
</textarea>
</div>
<iframe id="myframe" scrolling="yes" height="500px" width="100%" frameBorder="0" scrolling="no"></iframe>
</div>

<script src="https://code.jquery.com/jquery-3.6.1.min.js" crossorigin="anonymous"></script>
<script>
$(function () {
let htmlCon = $('#htmlContent').val();
var blob = new Blob([htmlCon], {
'type': 'text/html'
});

$('iframe').attr('src', URL.createObjectURL(blob));
$('#htmlContent').on('input propertychange', function (e) {
let htmlCon = $(e.target).val();
var blob = new Blob([htmlCon], {
'type': 'text/html'
});
$('iframe').attr('src', URL.createObjectURL(blob));
});
});
</script>
</body>

</html>

参考


iframe 实时预览 textarea 中的内容
https://hutaoren.cn/2022/10/08/iframe实时预览textarea中的内容/
作者
胡桃仁
发布于
2022年10月8日
许可协议