我在JSFiddle(http://jsfiddle.net/mattography/E4aqW/)中使用了以下代码,但无法弄清楚如何在"摄影"标题存储在JSON对象中并输出到div,类似于菜单设置的方式。
HTML
<div class="container">
<div class="row">
<div class="col-xs-3 col-sm-4">
<ul id="menu"></ul>
</div>
<div class="col-xs-6 col-sm-4">
<h1>Photography</h1> This is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
<div class="col-xs-3 col-sm-4">
<h3>Related Articles</h3>
<ul>
<li><a href="#">Nature Photography</a>
</li>
<li><a href="#">Tilt Shift Technique</a>
</li>
<li><a href="#">Night Time Photography</a>
</li>
</ul>
<div id='normalDialog'>
<input type="button" value="Login">
<!-- Modal Dialog Div -->
<div id="dialog-confirm"></div>
</div>
</div>
CSS
ul li {
list-style-type: square;
text-decoration:none;
}
#menu {
position: relative;
top: 30px;
}
.ui-menu {
width: 150px;
z-index: 1000;
}
JQUERY
$(function () {
var data = {
menu: [{
name: 'Cost',
link: '0',
sub: [{
name: 'Digital',
link: '0-0',
sub: null
}, {
name: 'Film',
link: '0-1',
sub: null
}, {
name: 'Processing',
link: '0-2',
sub: null
}]
}, {
name: 'Digital',
link: '1',
sub: [{
name: 'Cameras',
link: '1-0',
sub: null
}, {
name: 'Memory',
link: '1-1',
sub: null
}, {
name: 'HDR',
link: '1-2',
sub: null
}]
}, {
name: 'Film',
link: '2',
sub: [{
name: 'Types',
link: '2-0',
sub: null
}, {
name: 'Processing',
link: '2-1',
sub: null
}, {
name: 'ISO',
link: '2-2',
sub: null
}]
}, {
name: 'Filters',
link: '3',
sub: [{
name: 'Infrared',
link: '3-0',
sub: null
}, {
name: 'UV',
link: '3-1',
sub: null
}, {
name: 'Digital',
link: '3-2',
sub: null
}]
}, {
name: 'Process',
link: '4',
sub: [{
name: 'Digital',
link: '4-1',
sub: null
}, {
name: 'Film',
link: '4-2',
sub: null
}, {
name: 'History',
link: '4-3',
sub: null
}]
}]
};
var getMenuItem = function (itemData) {
var item = $("<li>")
.append(
$("<a>", {
href: '#' + itemData.link,
html: itemData.name
}));
if (itemData.sub) {
var subList = $("<ul>");
$.each(itemData.sub, function () {
subList.append(getMenuItem(this));
});
item.append(subList);
}
return item;
};
var $menu = $("#menu");
$.each(data.menu, function () {
$menu.append(
getMenuItem(this));
});
$menu.menu();
});
function fnOpenNormalDialog() {
var buf = "Username: " + "<input type='textbox'>" + "<br /><br />" +
"Password: " + "<input type='password'>";
// buf will be shown on the body of Dialog.
$("#dialog-confirm").html(buf);
// Define the Dialog and its properties.
$("#dialog-confirm").dialog({
resizable: false,
modal: true,
title: "Login",
height: 250,
width: 400,
buttons: {
"Login": function () {
$(this).dialog('close');
},
"Cancel": function () {
$(this).dialog('close');
}
}
});
}
// Define the click events of the divs.
$('#normalDialog').click(fnOpenNormalDialog);
答案 0 :(得分:0)
您应该使用mainArticle
和data
属性向heading
添加article
对象属性。然后,将标题和文章属性附加到要放置文章的元素中。请务必将标题放在<h1>
标记中。
这里是小提琴:http://jsfiddle.net/E4aqW/1/
var data = {
[...]
//Main Article Info
mainArticle: {
heading: "Photography",
article: "This is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
}
};
[...]
//Main Article DOM Manipulation
$("#mainArticle").append( $("<h1>").html(data.mainArticle.heading) ).append( data.mainArticle.article );