编辑,保存,自修改HTML文档;格式生成的HTML,JavaScript

错误:字符串转义,格式化htmljs由最初编辑,保存htmljs

例如,

a)如果在本地浏览器中打开“ saveFile.html”;

b)输入“ abc” textarea

c)点击save file按钮;

d)单击SaveSave File对话框;

e)file-*[date according to universal time].html保存到磁盘上;

f)file-*[date according to universal time].html在浏览器中打开;

g)在“ def”中输入textarea

h)重复d),e),f);

i)错误:第二个结果file-*[date according to universal time].html确实显示textarea包含“ abc

def”文本内容; button 显示在html

// at rendered `html` from second `file-*[date according to universal time].html`

// `textarea` containing "abc def" displayed here ,

// `button` _not_ displayed ; following string displayed following `textarea`:

');"console.log(clone);var file = new Blob([clone], {'type':'text/html'});a.href = URL.createObjectURL(file);a.download = 'file-' + new Date().getTime() + '.html';a.click();};

在第 26 行生成的“ saveFile.html”

+ "var clone = '<!doctype html>'+ document.documentElement.outerHTML.replace(/<textarea>.*<.+textarea>/, '<textarea>'+document.getElementsByTagName('textarea')[0].value+'<\/textarea>');"


“ saveFile.html” v 1.0.0

脚本

<!doctype html>

<html>

<!-- saveFile.html 1.0.0 2015 guest271314 edit, save `html` document -->

<head>

</head>

<body>

<textarea>

</textarea>

<button>save file</button>

<script type="text/javascript">

var saveFile = document.getElementsByTagName("button")[0];

var input = document.getElementsByTagName("textarea")[0];

var a = document.createElement("a");

saveFile.onclick = function(e) {

var clone = ["<!doctype html><head></head><body><textarea>"

+ input.value

+ "</textarea>"

+ "<button>save file</button>"

+ "<script type='text/javascript'>"

+ "var saveFile = document.getElementsByTagName('button')[0];"

+ "var input = document.getElementsByTagName('textarea')[0];"

+ "var a = document.createElement('a');"

+ "saveFile.onclick = function(e) {"

+ "var clone = '<!doctype html>'+ document.documentElement.outerHTML.replace(/<textarea>.*<.+textarea>/, '<textarea>'+document.getElementsByTagName('textarea')[0].value+'<\/textarea>');"

+ "console.log(clone);"

+ "var file = new Blob([clone], {'type':'text/html'});"

+ "a.href = URL.createObjectURL(file);"

+ "a.download = 'file-' + new Date().getTime() + '.html';"

+ "a.click();"

+ "};"

+ "</scr"+"ipt>"

+ "</body>"

+ "</html>"];

var file = new Blob([clone], {"type":"text/html"});

a.href = URL.createObjectURL(file);

a.download = "file-" + new Date().getTime() + ".html";

a.click();

};

</script>

</body>

</html>

回答:

你替换函数替换,直到/textarea>那就是在你的clone变量。它不会从第一个文件开始执行此操作,因为html的textarea之后有换行符。解决它的一种方法是newline在生成的html中添加一个字符。像这样:

var clone = ["<!doctype html><head></head><body><textarea>"

+ input.value

// add newline here

+ "</textarea>\n"

+ "<button>save file</button>"

+ "<script type='text/javascript'>"

+ "var saveFile = document.getElementsByTagName('button')[0];"

+ "var input = document.getElementsByTagName('textarea')[0];"

+ "var a = document.createElement('a');"

+ "saveFile.onclick = function(e) {"

+ "var clone = '<!doctype html>'+ document.documentElement.outerHTML.replace(/<textarea>.*<.+textarea>/, '<textarea>'+document.getElementsByTagName('textarea')[0].value+'<\/textarea>');"

+ "console.log(clone);"

+ "var file = new Blob([clone], {'type':'text/html'});"

+ "a.href = URL.createObjectURL(file);"

+ "a.download = 'file-' + new Date().getTime() + '.html';"

+ "a.click();"

+ "};"

+ "</scr"+"ipt>"

+ "</body>"

+ "</html>"];

以上是 编辑,保存,自修改HTML文档;格式生成的HTML,JavaScript 的全部内容, 来源链接: utcz.com/qa/405106.html

回到顶部