如何透過點擊按鈕隱藏某些 div?
P粉193307465
P粉193307465 2023-09-04 15:51:01
0
1
569
<p>我正在尋找一種方法,透過使用按鈕 onclick 事件來隱藏一定數量的 div。 </p> <p>我知道它可能與<code>.toggleClass('.AddCSSClassHere')</code> 有關,但我不太確定...我可以隱藏/顯示一個div,但不能父div 中的特定div。 jquery 來自我之前想要使用的程式碼片段。 當您按一下「顯示列」按鈕時,需要顯示或隱藏所有具有 hidethis 類別「」的 div</p> <p> <pre class="brush:js;toolbar:false;">$('button').click(function() { $(this).siblings('div').toggleClass('hidethis '); });</pre> <pre class="brush:css;toolbar:false;">* { margin: 0; padding: 0; } html, body { height: 100%; } body { background: #fff; padding: 0; margin: 0; font-family: Myriad-Pro, Arial, 'Varela Round', sans-serif; font-size: 16px; } .quotation { height: 100%; overflow: auto; } .main-div { position: sticky; top: 0; } .header-div { display: flex; padding: 5px 0; background-color: #fff; } .titles { display: flex; width: 100%; align-items: center; } .currencyinfo { width: fit-content; max-width: 100px; min-width: 100px; text-align: center; padding: 0; } .info { width: fit-content; max-width: 100px; min-width: 100px; text-align: center; padding: 10px 0; } .currency { max-width: 70px; min-width: 70px; } .table-info { display: flex; flex-direction: row; flex-wrap: wrap; width: 100%; overflow-y: scroll; } .main-div-info { display: flex; text-align: center; flex-direction: row; flex-wrap: wrap; width: 100%; } .div-info { display: flex; flex-direction: row; flex-wrap: wrap; width: 100%; padding: 10px 0; align-items: center; } .hidethis { display: none; }</pre> <pre class="brush:html;toolbar:false;"><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js" integrity ="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw==" crossorigin="anonymous="noref; <section class="quotation"> <div class="main-div"> <div class="header-div"> <div class="titles"> <div class="currencyinfo product-title"> <button class="show-this-data">show columns</button> </div> <div class="currencyinfo"></div> <div class="currencyinfo"></div> <div class="currencyinfo"></div> <div class="currencyinfo hidethis" style="border-left: 1px solid #bdbdbd;">Local</div> <div class="currencyinfo hidethis"></div> <div class="currencyinfo hidethis"></div> <div class="currencyinfo hidethis"></div> </div> </div> </div> <div class="main-div"> <div class="header-div">
產品名稱
ID
;
數量
期間
<div class="info hidethis">&nbsp;</div>
單價
單位成本
單位折扣
<div class="main-div"> <div class="header-div">
</節></pre> </p>
1
0
0
P粉193307465
P粉193307465

全部回覆(1)
P粉345302753

您需要上一層才能找到目標元素作為同級元素。縮排區塊級元素的內容有助於更好地視覺化。

如果您想切換所有容器中的所有此類元素,則需要引入一個新類別以在刪除該類別時追蹤它們,或者將它們全部選中並使用該元素列表。 p>

$(function() {
  const elsToToggle = $('.hidethis');

  $('.show-this-data').click(function() {
    $(this).parent().siblings('div').toggleClass('hidethis ');
  });

  $('.show-all-data').click(function() {
    elsToToggle.toggleClass('hidethis');
  });
});
* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

body {
  background: #fff;
  padding: 0;
  margin: 0;
  font-family: Myriad-Pro, Arial, 'Varela Round', sans-serif;
  font-size: 16px;
}

.quotation {
  height: 100%;
  overflow: auto;
}

.main-div {
  position: sticky;
  top: 0;
}

.header-div {
  display: flex;
  padding: 5px 0;
  background-color: #fff;
}

.titles {
  display: flex;
  width: 100%;
  align-items: center;
}

.currencyinfo {
  width: fit-content;
  max-width: 100px;
  min-width: 100px;
  text-align: center;
  padding: 0;
}

.info {
  width: fit-content;
  max-width: 100px;
  min-width: 100px;
  text-align: center;
  padding: 10px 0;
}

.currency {
  max-width: 70px;
  min-width: 70px;
}

.table-info {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 100%;
  overflow-y: scroll;
}

.main-div-info {
  display: flex;
  text-align: center;
  flex-direction: row;
  flex-wrap: wrap;
  width: 100%;
}

.div-info {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 100%;
  padding: 10px 0;
  align-items: center;
}

.hidethis {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js" integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<section class="quotation">

  <div class="main-div">
    <div class="header-div">
      <div class="titles">
        <div class="currencyinfo product-title">
          <button class="show-this-data">show columns in this block</button>
          <button class="show-all-data">show columns in all blocks</button>
        </div>
        <div class="currencyinfo"></div>
        <div class="currencyinfo"></div>
        <div class="currencyinfo"></div>
        <div class="currencyinfo hidethis" style="border-left: 1px solid #bdbdbd;">Local</div>
        <div class="currencyinfo hidethis"></div>
        <div class="currencyinfo hidethis"></div>
        <div class="currencyinfo hidethis"></div>
      </div>
    </div>
  </div>

  <div class="main-div">
    <div class="header-div">
      <div class="titles">
        <div class="info product-title">Product name</div>
        <div class="info">ID</div>
        <div class="info">Quantity</div>
        <div class="info">Periods</div>
        <div class="info hidethis">&nbsp;</div>
        <div class="info hidethis">Unit Price</div>
        <div class="info hidethis">Unit Cost</div>
        <div class="info hidethis">Unit Discount</div>
      </div>
    </div>
  </div>
  <div class="main-div">
    <div class="header-div">
      <div class="titles">
        <div class="info product-title"><i class="fa fa-chevron-down rotate" aria-hidden="true"></i><a href="#">Full Throttle</a></div>
        <div class="info">&nbsp;</div>
        <div class="info">1.0</div>
        <div class="info">1.0</div>
        <div class="info hidethis">USD</div>
        <div class="info hidethis">37.50</div>
        <div class="info hidethis">0.0</div>
        <div class="info hidethis">0.0</div>
      </div>
    </div>
  </div>
</section>
熱門專題
更多>
熱門文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板