javascript - 如何将文件内容加载到第三方编辑器前:(所见即所得编辑器)
问题描述
我在我的网络应用程序中使用第三方所见即所得编辑器。我想从文件中加载内容。如何加载内容?(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>
解决方案
您可以使用 javascript 中引入的 fetch api 来简化此操作。
fetch('Textfiles/Sample1.json')
.then(response => response.text())
.then(text => {
document.getElementById('NicEdit').innerHTML = text;
document.myForm.NicEdit.value += "Hello";
})
推荐阅读
- node.js - mongodb副本集如何与nodejs-mongoose一起工作?
- python - 在 python 中使用 matplotlib 将 5 x 5 数组(混淆矩阵)转换为表格
- javascript - 你如何通过反应和本地存储将数据保持在状态?
- reactjs - useState 未使用 socket.io 正确更新
- bazel - Bazel:将 Linux 远程执行平台与 Mac OS 本地平台混合
- c++ - Uno 的卡片类向量
- javascript - Firebase 身份验证检测用户是否来自重定向登录
- javascript - 如何从最大化 RAM 修复浏览器 blob 下载?
- curl - 我可以将笑话代码覆盖率添加到来自管道的 Bitbucket 拉取请求中的报告吗
- python - 如何从不同的内联查询中获取不同的 cache_times?