Uncaught TypeError: Cannot read property '0' of null<!DOCTYPE html> <html> <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>
<textarea id="show" spellcheck="true"># 猪</textarea>
<input type="file" id="file1">
<input type="button" id="btn" onclick="rec()" value="转换">
<script>
var filein = document.getElementById('file1'),
showbox = document.getElementById('show');
filein.addEventListener('change', function () {
var file = filein.files[0];
var reader = new FileReader();
reader.onload = function (e) {
showbox.innerHTML = data;
};
reader.readAsText(file, 'utf-8');
});
function rec() {
var textarea = document.getElementById('show');
textarea.innerHTML = main(textarea.value); //可以写入字符串
}
function main(evt) {
var char = evt.split("\n");
var matchArry;
var html = '';
var len = char.length;
for (var i = 0; i < len; i++) {
//数组储存模板
matchArry = char[i].match(/^#\s/); //切割成[""]
if (matchArry[0]) {
switch (matchArry[0]) { //输入了空的字符串和不匹配字符串则会报错
case '# ':
html += '<h1>' + add(char[i].substring(2)) + '</h1>';
break; default:
break; }
} else {
html += '<p>' + add(char[i]) + '</p>';
}
}
return html;
}
function add(str) {
str = str.replace(/\s/g, ' '
);
return str;
}
</script>
</body>
</html>
添加回答
举报
0/150
提交
取消