//显示注释api的JavaCIPRT代码
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(json => {
const title = json.title;
const body = json.body;
document.getElementById("printTitle").innerHTML = title;
document.getElementById("printBody").innerHTML = body;
});
//支持此js文件的HTML代码
<div >
TITLE
<span id="printTitle">
</span>
</div>
<div class="news-content-1">
BODY
<span id="printBody">
</span>
</div>
@Faizan Lambe,
请查看下面的javascript代码片段以了解您的查询。
let list = document.querySelector('#list');
function loadData(){
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(json => {
for(var i = 0; i < json.length; i++) {
var obj = json[I];
addList(obj);
}
});
}
// Add Lists
function addList(obj){
let li = document.createElement('li');
let p = document.createElement('p')
let t = document.createTextNode(obj.title);
p.appendChild(t);
li.appendChild(p);
list.appendChild(li);
}
-----HTML----
在html中,我将列表标记保存如下,
<ul id="list">
</ul>
使用addlist函数,将标题添加到此列表中。
请在您身边检查,如果它解决了您的问题,请标记为“已回答”。