-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (38 loc) · 1.58 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
let url = 'https://newsapi.org/v2/top-headlines?country=us&apiKey=a92fd65580354b0d8cee1ae550fcefc4';
fetch(url)
.then(response => response.json())
.then(result => {
console.log(result);
let data = result;
// console.log(data.articles[1].author);
Object.keys(data).forEach((item, key) => {
let arr = data.articles;
arr.forEach(item => {
console.log(data.articles[key].content);
let div = document.getElementById('app');
let img = document.createElement('img');
let h1 = document.createElement('h1');
let p = document.createElement('p');
let cont = document.createElement('div');
let date = document.createElement('p');
date.innerText = item.publishedAt;
h1.innerHTML = item.title;
p.innerHTML = item.description;
img.src = item.urlToImage;
cont.setAttribute('class', 'cont');
date.setAttribute('class', 'date');
cont.appendChild(h1);
cont.appendChild(date);
cont.appendChild(p);
cont.appendChild(img);
const visit = () => {
location.href = item.url;
preventDefault();
}
cont.onclick = () => {
visit();
}
div.appendChild(cont);
})
});
});