我在网站hokuco.com上的文本编辑器中添加文本时遇到问题。 这就是我所拥有的;
请尝试我的示例,您将看到文本框包含文本,我希望自动添加到java脚本中,编辑代码的人将看不到它
<div class = "fixed">
<center>
<table>
<tr><td>Type here</td></tr>
<tr>
<td colspan="3">
<textarea id="inputTextToSave" style="width:512px;height:256px">
<html>
<body>
<link rel="stylesheet" type="text/css" href="ceedweb.css" />
<!--type down there, by the way, this is a comment tag you won't need it-->
<body>
</html>
</textarea>
</td>
</tr>
<tr>
<td>Save As:</td>
<td><input id="inputFileNameToSaveAs"></input></td>
<td><button onclick="saveTextAsFile()"><img src="fl.gif" Save</button></td>
</tr>
<tr>
<td>Select file Load:</td>
<td><input type="file" id="fileToLoad"></td>
<td><button onclick="loadFileAsText()">Load File</button><td>
</tr>
</table>
</center>
<hr>
<script type='text/javascript'>
function saveTextAsFile()
{
var textToWrite = document.getElementById("inputTextToSave").value;
var textFileAsBlob = new Blob([textToWrite], {type:'text/html'});
var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null)
{
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else
{
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
function destroyClickedElement(event)
{
document.body.removeChild(event.target);
}
function loadFileAsText()
{
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("inputTextToSave").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
}
</script>
</div>
答案 0 :(得分:0)
如果您希望将该textarea的内容添加到用户的输入中而不将它们显示在textarea中,您可以在进一步处理之前简单地对textarea的值执行字符串连接:
var textToWrite = 'stuff that comes before';
textToWrite += document.getElementById("inputTextToSave").value;
textToWrite += 'stuff that comes after';
// And then do all the rest.
var textFileAsBlob = new Blob([textToWrite], {type:'text/html'});