js如何在textarea中添加文字

how to add text to textarea in js

我在向我网站上的文本编辑器添加文本时遇到问题 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">
   &lt;html&gt; 
   &lt;body&gt; 
   &lt;link rel="stylesheet" type="text/css" href="ceedweb.css" /&gt;
   &lt;!--type down there, by the way, this is a comment tag you won't need it--&gt;
   
   
   &lt;body&gt;
   &lt;/html&gt;
   </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>

如果您希望将该文本区域的内容添加到用户的输入中而不让它们出现在文本区域内,您可以在进一步处理之前简单地对文本区域的值执行字符串连接:

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'});