首页 > 解决方案 > 如何将文件内容加载到第三方编辑器前:(所见即所得编辑器)

问题描述

我在我的网络应用程序中使用第三方所见即所得编辑器。我想从文件中加载内容。如何加载内容?(javascript 或 React js)

var filecontent; 

function selection(){ 
  var filevalue=document.getElementById('files').value; 
  if(filevalue=="A"){ 
    fetchDetailsA(); 
  } 
}

function fetchDetailsA() {

  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {

    if (this.readyState == 4 && this.status == 200) {
      var myObj = JSON.parse(this.responseText);
      filecontent = this.responseText;
      document.getElementById('NicEdit').innerHTML = filecontent;
      document.myForm.NicEdit.value += "Hello";
      alert(document.myForm.NicEdit.value)
    }
  };
  xhttp.open("GET", "Textfiles/Sample1.json", true);
  xhttp.send();
}
<form name="myForm">
  <textarea id="NicEdit" cols="80" rows="5"></textarea>
</form>

标签: javascriptreactjs

解决方案


您可以使用 javascript 中引入的 fetch api 来简化此操作。

fetch('Textfiles/Sample1.json')
  .then(response => response.text())
  .then(text => {
      document.getElementById('NicEdit').innerHTML = text;
      document.myForm.NicEdit.value += "Hello";
  })

推荐阅读