Moin, ich habe dafür zwei Ansätze, bin aber unsicher, welcher der sauberere wäre...
1.
2.
Sind beide Ansätze zu 100% äquivalent - oder gibt es Unterschiede? Hat ein a-Element nicht auch ein text-Property?
1.
HTML:
<span id="news1"></span>
<script>
fetch('headlines.json', {
method: 'GET'
})
.then(function (response) { return response.json(); })
.then(function (json) {
json = json.results;
let html = '';
for (let i in json) {
let a = json[i];
html += '<a href="' + a.link + '">' + a.title + '</a> - ';
}
document.getElementById('news1').innerHTML = html;
});
</script>
2.
HTML:
<span id="news1"></span>
<script>
fetch('headlines.json', {
method: 'GET'
})
.then(function (response) { return response.json(); })
.then(function (json) {
json = json.results;
let span = document.getElementById('news1');
let delimiter = ' - ';
for (let i in json) {
let article = json[i];
let a = document.createElement('a');
a.href = article.link;
a.appendChild(document.createTextNode(article.title));
span.appendChild(a);
span.innerHTML += delimiter;
}
});
</script>
Sind beide Ansätze zu 100% äquivalent - oder gibt es Unterschiede? Hat ein a-Element nicht auch ein text-Property?