这有帮助吗
<button id="b1" type="button">Get All Albums</button>
<button id="b2" type="button">Get All Albums By Artist</button>
<button id="b3" type="button">Get All By Category</button>
<table border="1">
<thead>
<tr>
<th>Title</th>
<th>Category</th>
<th>Artist</th>
<th>Tracks</th>
</tr>
</thead>
<tbody id="albums"></tbody>
</table>
var b1 = document.getElementById("b1"),
b2 = document.getElementById("b2"),
b3 = document.getElementById("b3"),
results = document.getElementById("albums"),
albums = [];
function compareTitle(a, b) {
if (a.title < b.title) {
return -1;
}
if (a.title > b.title) {
return 1;
}
return 0;
}
function compareArtist(a, b) {
if (a.artist < b.artist) {
return -1;
}
if (a.artist > b.artist) {
return 1;
}
return 0;
}
function compareCategory(a, b) {
if (a.category < b.category) {
return -1;
}
if (a.category > b.category) {
return 1;
}
return 0;
}
function addAlbum(title, category, artist, tracks) {
var album = {
"title": title,
"category": category,
"artist": artist,
"tracks": tracks
};
albums.push(album);
}
addAlbum("d", "z", "s", [1, 2, 3]);
addAlbum("c", "y", "s", [1, 2, 3]);
addAlbum("b", "x", "t", [1, 2, 3]);
addAlbum("a", "x", "u", [1, 2, 3]);
function displayAll() {
results.innerHTML = "";
albums.sort(compareTitle);
albums.forEach(function (album) {
var tr = document.createElement("tr"),
td1 = document.createElement("td"),
td2 = document.createElement("td"),
td3 = document.createElement("td"),
td4 = document.createElement("td");
td1.textContent = album.title;
td2.textContent = album.category;
td3.textContent = album.artist;
td4.textContent = album.tracks.toString();
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
tr.appendChild(td4);
results.appendChild(tr);
});
}
function displayArtist() {
results.innerHTML = "";
albums.sort(compareArtist);
albums.forEach(function (album) {
var tr = document.createElement("tr"),
td1 = document.createElement("td"),
td2 = document.createElement("td"),
td3 = document.createElement("td"),
td4 = document.createElement("td");
td1.textContent = album.title;
td2.textContent = album.category;
td3.textContent = album.artist;
td4.textContent = album.tracks.toString();
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
tr.appendChild(td4);
results.appendChild(tr);
});
}
function displayCategory() {
results.innerHTML = "";
albums.sort(compareCategory);
albums.forEach(function (album) {
var tr = document.createElement("tr"),
td1 = document.createElement("td"),
td2 = document.createElement("td"),
td3 = document.createElement("td"),
td4 = document.createElement("td");
td1.textContent = album.title;
td2.textContent = album.category;
td3.textContent = album.artist;
td4.textContent = album.tracks.toString();
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
tr.appendChild(td4);
results.appendChild(tr);
});
}
b1.addEventListener("click", displayAll, false);
b2.addEventListener("click", displayArtist, false);
b3.addEventListener("click", displayCategory, false);
在jsfiddle 上