jQuery를 통해 JSON 요청을 호출하여 메뉴를 작성하여 즉석에서 만들어진 동적 메뉴를 작성하려고합니다. 예를 들어JSON에서 메뉴 만들기
<ul class="menu">
<li>
<a href="/Organisations/201/NewJournalEntry" title="New Journal Entry">
<span class="icon journal-new">New Journal Entry</span>
</a>
</li>
<li>
<a href="/Organisations/201/People" title="View People">
<span class="icon people">People</span>
</a>
</li>
<li class="sep">
<a href="/Organisations/201/Edit" title="Edit">
<span class="icon edit">Edit</span>
</a>
</li>
</ul>
하고 다음 JSON :
{"menu": {
"id": "organisation-201",
"class": "menu",
"content": {
"menuitem": [
{"text" : "New Journal Entry", "title" : "New Journal Entry", "href" : "/Organisations/201/NewJournalEntry", "liClass" : "", "icon" : "icon journal-new" },
{"text" : "View People", "title" : "View People", "href" : "/Organisations/201/People", "liClass" : "", "icon" : "icon people" },
{"text" : "Edit", "title" : "Edit", "href" : "/Organisations/201/Edit", "liClass" : "sep", "icon" : "icon edit" }
]
}
}}
은 무엇 이렇게하는 가장 좋은 방법이 될 것이라고 나는 다음과 같은 계획 메뉴 구조를 가지고? 쿼리 될 URL은 '/ MenuBuilder/organization-201.json`과 같습니다.
$ .getJSON 메서드를 살펴 보았지만 올바른 것이 무엇입니까? 사람이 ... ...
$(document).ready(function() {
// First we connect to the JSON file
$.getJSON('menu.json', function(data)
{
// Setup the items array
var items = [];
// For each line of data
$.each(data, function(key, val)
{
items.push('<li class="' + liClass + '"><a href="' + href + '"><span class="' + icon +'">' + text + '</span></a></li>');
});
$('<ul/>', {
html: items.join('')
}).appendTo('#buildHere');
});
// END json
});
무엇 너 질문 있니? 서버에서 json을 검색하거나 DOM 요소를 (또는 둘 다) 생성하고 있습니까? – mgibsonbr
JSON – Cameron
에서 메뉴 만들기 아래에서 내 대답을 소개합니다. 필요에 맞게 쉽게 완성 할 수 있습니다. 그러나 장기적으로 Rody van Sambeek과 같은 템플릿 플러그인을 배우는 것이 더 좋을 것이라고 생각하지만 개인적인 경험이 없으므로 확신 할 수 없습니다. – mgibsonbr