如何在Javascript中使用json文件替换图像元素

如何在Javascript中使用json文件替换图像元素,javascript,json,replace,fetch,Javascript,Json,Replace,Fetch,我在尝试使用“替换”替换文档中的某个元素时遇到问题。我创建了一个名为c的常量,它使用c.replace获取一个特定的图像,但是当我运行代码时,我得到一个错误,指出“c.replace不是一个函数”,有人能提供一些指导吗?以下是我正在使用的代码: function imgenlarger(){ const t = "test.json" fetch(t) .then(function (response) { retur

我在尝试使用“替换”替换文档中的某个元素时遇到问题。我创建了一个名为c的常量,它使用c.replace获取一个特定的图像,但是当我运行代码时,我得到一个错误,指出“c.replace不是一个函数”,有人能提供一些指导吗?以下是我正在使用的代码:




        function imgenlarger(){
        const t = "test.json"




fetch(t)
  .then(function (response) {
    return response.json();
  })
  .then(function (jsonObject) {
    const color = jsonObject['color'];
    for (let i = 0; i < color.length; i++ ) {

        let colorTitle = document.createElement('section');
        let colorName = document.createElement('h2');
        colorName.textContent = color[i];
        colorTitle.appendChild(colorName);
        document.querySelector('div.api-wrapper').appendChild(colorTitle);
        const c = color[i];
        document.getElementById('bigpic').src = c.replace('90x90', '225x225');
        
    }
  });










        
        }






我发现我做错了什么。我不得不更改json文件以使其更有意义,因此我重新编写如下:

{
    "color":[
        {
            "name": "red",
            "image": "https://upload.wikimedia.org/wikipedia/commons/8/8e/Flag_Vice_Admiral_of_Red_1805_to_1864.png"
        },
        {
            "name": "blue",
            "image": "https://static.onecms.io/wp-content/uploads/sites/28/2017/05/blue0517.jpg"
        },
        {
            "name": "yellow",
            "image": "https://professionals.tarkett.com/media/img/M/TH_3917013_3707013_3708013_3912013_3914013_800_800.jpg"
        },
        {
            "name": "green",
            "image": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Solid_green.svg/1200px-Solid_green.svg.png"
        }
    ]
}
并将js文件更新为:

function imgenlarger() {
   const t = "test.json"
   fetch(t)
       .then(function (response) {
           return response.json();
       })
       .then(function (jsonObject) {
           const color = jsonObject['color'];
           for (let i = 0; i < color.length; i++) {

               let colorTitle = document.createElement('section');
               let colorName = document.createElement('h2');
               colorName.textContent = color[i].name;
               colorTitle.appendChild(colorName);
               document.querySelector('div.api-wrapper').appendChild(colorTitle);

               var img = document.createElement("img");
               img.src = color[i].image;
               var oldImg = document.getElementById('bigpic');
               document.querySelector('div.api-wrapper').replaceChild(img, oldImg);
           }
       });        
   }

函数imgenlarger(){
const t=“test.json”
取回(t)
.然后(功能(响应){
返回response.json();
})
.then(函数(jsonObject){
const color=jsonObject['color'];
for(设i=0;i
c
一个字符串,还是其他东西
String.prototype.replace()
只存在于字符串中。您能提供您正在使用的json内容吗?如果它很大,您能提供它的一部分吗?replace函数只用于字符串,可能您的变量不是字符串。谢谢大家。我试图用json文件中的另一个图像替换一个图像。我做了一些编辑来反映这一点。一件没有意义的事情是,你在数组上循环,但只将源设置为一个元素。因此,它将永远是最后一个索引
const c=color[i];控制台日志(i,c)它是您所期望的吗?
function imgenlarger() {
   const t = "test.json"
   fetch(t)
       .then(function (response) {
           return response.json();
       })
       .then(function (jsonObject) {
           const color = jsonObject['color'];
           for (let i = 0; i < color.length; i++) {

               let colorTitle = document.createElement('section');
               let colorName = document.createElement('h2');
               colorName.textContent = color[i].name;
               colorTitle.appendChild(colorName);
               document.querySelector('div.api-wrapper').appendChild(colorTitle);

               var img = document.createElement("img");
               img.src = color[i].image;
               var oldImg = document.getElementById('bigpic');
               document.querySelector('div.api-wrapper').replaceChild(img, oldImg);
           }
       });        
   }