0
erstellen Ich verwende JSON, um HTML-Vorlage zu erstellen, aber ich habe Probleme beim Erstellen von Struktur von JavaScript zu HTML. Dann habe ich meinen Code untenwie Struktur HTML von verschachtelten JSON
$(document).ready(function(){
showList();
});
function showList(){
var myObj = {
"myresult" : [
{
"A" : [
{
"title" \t \t : \t "this is title A1",
"konten" \t : \t "this is konten A1",
"desc" \t \t : \t "this is description A1"
},
{
"title" \t \t : \t "this is title A2",
"konten" \t : \t "this is konten A2",
"desc" \t \t : \t "this is description A2"
},
{
"title" \t \t : \t "this is title A3",
"konten" \t : \t "this is konten A3",
"desc" \t \t : \t "this is description A3"
},
],
"B" : [
{
"title" \t \t : \t "this is title B1",
"konten" \t : \t "this is konten B1",
"desc" \t \t : \t "this is description B1"
},
{
"title" \t \t : \t "this is title B2",
"konten" \t : \t "this is konten B2",
"desc" \t \t : \t "this is description B2"
},
{
"title" \t \t : \t "this is title B3",
"konten" \t : \t "this is konten B3",
"desc" \t \t : \t "this is description B3"
},
],
"C" : [
{
"title" \t \t : \t "this is title C1",
"konten" \t : \t "this is konten C1",
"desc" \t \t : \t "this is description C1"
},
{
"title" \t \t : \t "this is title C2",
"konten" \t : \t "this is konten C2",
"desc" \t \t : \t "this is description C2"
},
{
"title" \t \t : \t "this is title C3",
"konten" \t : \t "this is konten C3",
"desc" \t \t : \t "this is description C3"
},
]
}
]
}
$.each(myObj, function(data){
$.each(this, function(index, obj){
$.each(obj, function(key, val){
listRes(key);
});
$.each(obj.A, function(key, val){
listRes(val.title);
});
$.each(obj.B, function(key, val){
listRes(val.title);
});
$.each(obj.C, function(key, val){
listRes(val.title);
});
});
});
function listRes(title){
var p = '<p>'+title+'</p>';
var h2 = '<h2>'+title+'</h2>';
var alpha = $('<div class="alphabet">' + h2 + p +'</div>');
$('#result').append(alpha);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='result'>
</div>
Mein erwartetes Ergebnis ist
<div id="result">
<div class="alphabet">
<h2>A</h2>
<p>this is title A1</p>
<p>this is title A2</p>
<p>this is title A3</p>
</div>
<div class="alphabet">
<h2>B</h2>
<p>this is title B1</p>
<p>this is title B2</p>
<p>this is title B3</p>
</div>
<div class="alphabet">
<h2>C</h2>
<p>this is title C1</p>
<p>this is title C2</p>
<p>this is title C3</p>
</div>
</div>
Wie kann ich die richtige Schleife Ergebnis wie diese zu haben?
es wie Magie funktioniert, ich verwirrend, wie HTML-Struktur in Javascript-Schleife. Jetzt habe ich eine gute Erklärung aus Ihrer Antwort. Danke für die Erklärung. – rnDesto
Sie sind willkommen Kumpel :) –