-
Notifications
You must be signed in to change notification settings - Fork 0
/
在线运行代码.html
116 lines (95 loc) · 3.35 KB
/
在线运行代码.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
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>在线运行HTML代码</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.buttons-container {
margin-bottom: 10px;
}
.buttons-container input[type="file"] {
margin-right: 10px;
}
.code-container {
width: 100%;
height: 700px;
border: 1px solid #ffffff;
margin-bottom: 20px;
}
#html-code {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<h1>在线运行HTML代码</h1>
<div class="buttons-container">
<input type="file" id="fileInput" style="display: none;">
<button onclick="runCode()">运行</button>
<button onclick="saveCode()">保存</button>
<button onclick="clearCode()">清空</button>
</div>
<div class="code-container" ondrop="handleDrop(event)" ondragover="handleDragOver(event)">
<textarea id="html-code" placeholder="或在这里输入你的HTML代码"></textarea>
</div>
<script>
document.getElementById('fileInput').addEventListener('change', function(event) {
var fileInput = event.target;
var file = fileInput.files[0];
var reader = new FileReader();
reader.onload = function(event) {
var fileContent = event.target.result;
document.getElementById('html-code').value = fileContent;
};
reader.readAsText(file);
});
function runCode() {
var htmlCode = document.getElementById('html-code').value;
var newWindow = window.open();
newWindow.document.open();
newWindow.document.write(htmlCode);
newWindow.document.close();
}
function saveCode() {
var htmlCode = document.getElementById('html-code').value;
// 弹出输入框,让用户输入文件名
var fileName = prompt("请输入文件名(包括后缀)", "code.html");
// 如果用户点击取消按钮,则不保存文件
if (fileName === null) {
return;
}
// 创建Blob对象并保存文件
var blob = new Blob([htmlCode], { type: 'text/plain' });
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = fileName;
a.click();
URL.revokeObjectURL(url);
}
function clearCode() {
document.getElementById('html-code').value = '';
document.getElementById('fileInput').value = '';
}
function handleDrop(event) {
event.preventDefault();
var file = event.dataTransfer.files[0];
var reader = new FileReader();
reader.onload = function(event) {
var fileContent = event.target.result;
document.getElementById('html-code').value = fileContent;
};
reader.readAsText(file);
}
function handleDragOver(event) {
event.preventDefault();
}
</script>
</body>
</html>