亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

如何在頁(yè)面上篩選內(nèi)容卡?
P粉680487967
P粉680487967 2024-03-30 19:18:27
0
1
666

我創(chuàng)建了一篇包含三個(gè)類別的部落格文章:(數(shù)位行銷類別、提示和建議類別、加密貨幣類別)

我想做的是過(guò)濾它。

例如,如果我想查看數(shù)位行銷類別的所有文章,我將點(diǎn)擊此按鈕來(lái)顯示所有數(shù)位行銷類別並隱藏其他類別。

這是我的程式碼範(fàn)例。問(wèn)題是我的過(guò)濾器不起作用。問(wèn)題出在哪裡?

我嘗試添加 JavaScript 使其工作,但仍然不起作用。問(wèn)題出在哪裡?

document.addEventListener("DOMContentLoaded", () => {
  "use strict";

  /**
   * Article Category Filter
   */
  const filterButtons = document.querySelectorAll('.blog-filters button');
  const articleCards = document.querySelectorAll('.article-card');

  filterButtons.forEach(button => {
    button.addEventListener('click', () => {
      const selectedCategory = button.getAttribute('data-category');

      articleCards.forEach(card => {
        const cardCategory = card.getAttribute('data-category');

        if (selectedCategory === 'all' || selectedCategory === cardCategory) {
          card.style.display = 'block';
        } else {
          card.style.display = 'none';
        }
      });
    });
  });
}); // this line added by community
<section id="blog" class="blog">
  <div class="row">
    <div class="col-md-8">
      <div class="posts-list">
        <div class="row">
          <div class="col-lg-6 col-md-6 d-md-flex article-card" data-category="digital-marketing-category">
            Blog Card
          </div>
          <!-- Article Card -->

          <div class="col-lg-6 col-md-6 d-md-flex article-card" data-category="tips-and-advice-category">
            Blog Card
          </div>
          <!-- Article Card -->

          <div class="col-lg-6 col-md-6 d-md-flex article-card" data-category="cryptocurrency-category">
            Blog Card
          </div>
          <!-- Article Card -->
        </div>
      </div>
    </div>

    <div class="col-md-4">
      <div class="sidebar">
        <h3 class="sidebar-title">Article Categories</h3>
        <div class="blog-filters">
          <button data-category="filter-all">All</button>
          <button data-category="digital-marketing-category">Digital Marketing</button>
          <button data-category="tips-and-advice-category">Tips & Advice</button>
          <button data-category="cryptocurrency-category">Cryptocurrency</button>
        </div>
      </div>
    </div>
  </div>
</section>

P粉680487967
P粉680487967

全部回覆(1)
P粉538462187

在您的程式碼中,您將「全部過(guò)濾」按鈕的 data-category 屬性定義為 data-category="filter-all"。因此,當(dāng)按一下「全部」按鈕時(shí),JavaScript 程式碼中的 selectedCategory 變數(shù)將被設(shè)定為「filter-all」。

但是,在按類別過(guò)濾文章的 if 語(yǔ)句中,該語(yǔ)句與「all」進(jìn)行比較,這與「filter-all」不匹配,因此不會(huì)顯示任何文章。

這是您修正後的程式碼:

if (selectedCategory === 'filter-all' || selectedCategory === cardCategory) {
  card.style.display = 'block';
} else {
  card.style.display = 'none';
}

此外,您提供的 JS 在最後缺少 });,不確定這是否只是您不小心錯(cuò)過(guò)了該行想法:)

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板