javascript - 如何从 html 类中删除额外的 ""组?

标签 javascript html

我试图将受感染的变量字符串添加为 html 类,但我得到了一组额外的“”。

    setInterval(function(){
        return function(){
            fetch('http://127.0.0.1:5000/showTable')
            .then(response => response.text())
            .then(data => {
                document.getElementById("table").innerHTML = data;
                document.getElementById("table").className = data;
            });
        };
    }(), 1000);

这在检查器中给了我这个类:

<p id="table" class=" "Green0" " >"Green0"</p>

如何删除多余的“”?

最佳答案

我认为 API 返回的是 JSON,而不是纯文本。您需要解析它。

setInterval(function(){
    return function(){
        fetch('http://127.0.0.1:5000/showTable')
        .then(response => response.json())
        .then(data => {
            document.getElementById("table").innerHTML = data;
            document.getElementById("table").className = data;
        });
    };
}(), 1000);

关于javascript - 如何从 html 类中删除额外的 ""组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61049486/

相关文章:

javascript - 使用rails如何将 Controller 中的变量同步到 View ,其中 Controller 是更新的发起者

javascript - Angular - 同步 XMLHttpRequest 加载 Assets 的 JSON

javascript - 如何在 Angular 4 的 FormArray 中禁用 FormControl

html - 更改多个特定数据的颜色

javascript - GeoJSON - 在表格中显示所有功能

html - 向上移动图像以与 slider 对齐?

html - 尝试在 <button> 中使用图像和文本

Javascript 测试一个变量是否被设置

javascript - Bootstrap 移动导航下拉菜单在打开时跳转

html - Paypal 网址显示的是滑稽的字符