我想尝试使用HTML上的小屏幕输出文件。每次我点击按钮我想要替换文件中的文本。这是我写的代码:写入html中的文本文件
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>This is Web App</title>
</head>
<script>
function createDoc()
{
var doc=document.open("answer.txt","replace");
var txt="<html><body>You clicked Allow</body></html>";
doc.write(txt);
doc.close();
}
function createDoc2()
{
var doc=document.open("answer.txt","replace");
var txt="<html><body>You clicked Deny</body></html>";
doc.write(txt);
doc.close();
}
</script>
<body>
<h1> This is the visitor at your house</h1>
<img src = "./images/chef.gif" width="130" height="101" />
<br />
<button name="Yes" type="button" onclick="createDoc()">Allow! </button>
<button name="No" type="button" onclick="createDoc2()">Deny! </button>
</body>
</html>
我知道这不是正确的做法,但我正在努力学习。请帮助我并指出我的错误,并告诉我如何尽可能纠正错误。我知道可能会有很多。我现在只是想教自己。非常感谢您的帮助。
我不知道这甚至可能o.O –
呃,那是因为你不能。有HTML5 File API,但这听起来不像你想要做的。这将是一个巨大的安全风险... – tjameson
@tjameson我有一个想法,它不会工作,但想尝试。有没有办法像发送Get请求或其他内容? –