将香草js转换为jquery

时间:2020-05-11 04:38:33

标签: javascript jquery

这是我的普通js

fetch("https://gist.githubusercontent.com/nabz123/9bef14fe98f95367887f827c3c5a0906/raw/01869dca62142c5ec0092316436cf3a7ecd7a8ec/topartists.json").then(
    res=>{
        res.json().then(
            data=>{
                var element ="";
                data.forEach((item) => {
                    element += '<div class="xxx"><img class="imgflex" src="' + item.src + '"><p class="imageHead">'
                    + item.name+'</p> <p class="imageText">' + item.plays+ '</p></div>';                  
                });
                document.getElementById("artist_container").innerHTML = element;
                console.log(data);
            }
        )
    }
)

我正在尝试将其转换为以下jQuery

$(_ =>{
    $.getJSON("https://gist.githubusercontent.com/nabz123/9bef14fe98f95367887f827c3c5a0906/raw/01869dca62142c5ec0092316436cf3a7ecd7a8ec/topartists.json")
    ,function(data){
        let element ="";
        data.forEach((item) => {
            element += '<div class="xxx"><img class="imgflex" src="' + item.src + '"><p class="imageHead">'
            + item.name+'</p> <p class="imageText">' + item.plays+ '</p></div>';
        });
        document.getElementById("artist_container").innerHTML=element;
    }
});

我需要帮助修复我的jquery,谢谢

1 个答案:

答案 0 :(得分:1)

更改

data.forEach((item) => { code });

$.each(data, (i, item) => { code });

更改

document.getElementById("artist_container") = element;

$("#artist_container").html(element);