JSON 형식
[{"name": "Sri Lanka","region":"Asia"}, {"name": "India","region":"Asia"}, {"name": "kenya","region":"Africa"}, {"name": "Zimbabwe","region":"Africa"}]
아래와 같이 표시하고 싶었습니다.
짐바브웨
아시아
시험
var data = [{
"name": "Sri Lanka",
"region": "Asia"
}, {
"name": "India",
"region": "Asia"
}, {
"name": "Kenya",
"region": "Africa"
}, {
"name": "Zimbabwe",
"region": "Africa"
}];
$.each(data, function(key, val) {
// if `body` does not already have `ul` having
// `class` `region` , append `ul` with first `name`
// utilize `b` element as "header" for `ul` ,
// to render `region` "header" above `ul` `li` `name`s
if (!$("body ul." + val.region).is("*")) {
$("<ul />", {
"class": val.region,
"html": "<li>" + val.name + "</li>"
})
.appendTo("body")
.before("<b class=" + val.region + ">" + val.region + "</b>");
} else {
// if `b` `textContent` is `region` ,
// append remaining `name` to `ul` that follows `b`
$("b." + val.region).each(function() {
if (this.textContent === val.region) {
$(this).next("ul").append("<li>" + val.name + "</li>")
}
})
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다