1

我是 JavaScript 新手,我正在尝试使用纯 JavaScript 和 HTML 创建一组硬编码专辑,每个专辑都有一个title, category, artist, tracks[]. 现在我被卡住的地方是试图显示我的按钮数组onclick,并根据按下的按钮以不同的方式显示。我有三个按钮:

  1. 获取所有专辑
  2. 按艺术家获取所有专辑
  3. 按类别获取所有。

当我单击全部时,我会在项目符号列表中按名称获取所有专辑。按艺术家我只能按艺术家获得所有专辑。这是我的代码。我正在使用外部 JavaScript 文件。非常感谢任何帮助,但请不要使用 JQuery 我想在纯 JavaScript 中执行此操作。

这是我到目前为止所拥有的 jsbin

这里

4

1 回答 1

2

这有帮助吗

<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 上

于 2013-04-19T19:38:13.427 回答