亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何為同一頁面創建多個“多讀”和“少讀”按鈕?

如何為同一頁面創建多個“多讀”和“少讀”按鈕?

慕妹3146593 2022-08-04 17:03:58
我正在處理一個相當大的項目,我在閱讀更多按鈕時遇到問題,我對javascript很陌生,并且仍然試圖解決這個問題。我已經為第一個按鈕工作的功能,但它對其他人不起作用,你能告訴我我做錯了什么嗎。$(document).ready(function() {  $("#read").click(function() {    $(this).prev().toggle();    $(this).siblings('.dots').    toggle("read");    if ($(this).text() == 'Read More') {      $(this).text('Read Less');    } else {      $(this).text('Read More');    }  });});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><h1>The Hound Group</h1><div class="container">  <h3>The Sighthounds</h3>  <p>    By definition, a sighthound is simply a dog breed that hunts primarily by sight and speed rather than by scent because of this they are sometimes refered to as Gazehounds. Because sighthounds hunt by sight, they need to have the speed and agility to keep    up with their prey in order to keep them in sight. Sighthound breeds typically have lean bodies, long legs, and a flexible back – they also have deep chests to support their unusually large hearts and lungs. To help you picture the typical sighthound    body, here is a list of breeds that are categorized as sighthounds:    <h4>Afghan Hound:</h4>    <p>      Wow very biscit stop it fren very hand that feed shibe heckin, many pats. Shooberino h*ck blep shoob sub woofer very taste wow shoober, snoot heckin mlem smol borking doggo with a long snoot for pats. Heck I am bekom fat pupperino borkdrive length boy,      such treat maximum borkdrive wow very biscit. Borking doggo clouds much ruin diet pupperino tungg super chub, very hand that feed shibe you are doin me a concern boofers它適用于第一段,但不適用于第二段,你能告訴我為什么或者是否有更好的方法來做到這一點嗎?謝謝
查看完整描述

2 回答

?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

首先 - 不要多次使用相同的ID。我的意思是 - 復制是錯誤的。其次 - 元素的結構需要更加分離。每個邏輯塊都應該用它自己的父元素來覆蓋。試試這個:id="read"


$(document).ready(function(){

   $(".read").click(function(){

      $(this).parent().find('.showmore').toggle();

        if($(this).text() =='Read More'){

          $(this).text('Read Less');

        }

        else{

          $(this).text('Read More');

        }

    });

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="toggle-container">

  <div class="first">

    <h3>The Sighthounds</h3>

    <p>

    By definition, a sighthound is simply a dog breed that hunts primarily by 

    sight and speed rather than by scent because of this they are sometimes 

    refered to as Gazehounds. Because sighthounds hunt by sight, they need to 

    have the speed and agility to keep up with their prey in order to keep 

    them in sight. Sighthound breeds typically have lean bodies, long legs, 

    and a flexible back – they also have deep chests to support their 

    unusually large hearts and lungs. To help you picture the typical 

    sighthound body, here is a list of breeds that are categorized as 

    sighthounds:

    </p>

    <span class="dots">...</span>

  </div>

  <div class="showmore" style="display: none;">

   <h5>Origin</h5>

   <p>

    Doggo ipsum smol borking doggo with a long snoot for pats bork mlem 

    heckin much ruin diet lotsa pats I am bekom fat, doing me a frighten most 

    angery pupper I have ever seen pats waggy wags vvv. big ol pupper big ol. 

    Ur givin me a spook smol borking doggo with a long snoot for pats big ol 

    pupper long bois ur givin me a spook bork smol adorable doggo, the 

    neighborhood pupper doing me a frighten puggorino tungg ur givin me a 

    spook. Blep the neighborhood pupper heckin good boys big ol pupper noodle 

    horse, doge big ol blep. noodle horse shibe maximum borkdrive. Bork you 

    are doing me a frighten boof pats, noodle horse extremely cuuuuuute you 

    are doing me the shock borkf, boof borkf.

   </p>

  </div>

  <button type="button" class="read btn btn-info btn-xs">Read More</button>

</div>


<div class="toggle-container">

  <div class="first">

    <h3>The Sighthounds</h3>

    <p>

    By definition, a sighthound is simply a dog breed that hunts primarily by 

    sight and speed rather than by scent because of this they are sometimes 

    refered to as Gazehounds. Because sighthounds hunt by sight, they need to 

    have the speed and agility to keep up with their prey in order to keep 

    them in sight. Sighthound breeds typically have lean bodies, long legs, 

    and a flexible back – they also have deep chests to support their 

    unusually large hearts and lungs. To help you picture the typical 

    sighthound body, here is a list of breeds that are categorized as 

    sighthounds:

    </p>

    <span class="dots">...</span>

  </div>

  <div class="showmore" style="display: none;">

   <h5>Origin</h5>

   <p>

    Doggo ipsum smol borking doggo with a long snoot for pats bork mlem 

    heckin much ruin diet lotsa pats I am bekom fat, doing me a frighten most 

    angery pupper I have ever seen pats waggy wags vvv. big ol pupper big ol. 

    Ur givin me a spook smol borking doggo with a long snoot for pats big ol 

    pupper long bois ur givin me a spook bork smol adorable doggo, the 

    neighborhood pupper doing me a frighten puggorino tungg ur givin me a 

    spook. Blep the neighborhood pupper heckin good boys big ol pupper noodle 

    horse, doge big ol blep. noodle horse shibe maximum borkdrive. Bork you 

    are doing me a frighten boof pats, noodle horse extremely cuuuuuute you 

    are doing me the shock borkf, boof borkf.

   </p>

  </div>

  <button type="button" class="read btn btn-info btn-xs">Read More</button>

</div>



查看完整回答
反對 回復 2022-08-04
?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

我正在考慮一種不同的方法,也許在JS中使用選項卡。我使用您的實際內容編輯了代碼段,以便您可以看到它的顯示方式。參考此:https://www.w3schools.com/howto/howto_js_tabs.asp


function openDef(evt, cityName) {

  var i, tabcontent, tablinks;

  tabcontent = document.getElementsByClassName("tabcontent");

  for (i = 0; i < tabcontent.length; i++) {

    tabcontent[i].style.display = "none";

  }

  tablinks = document.getElementsByClassName("tablinks");

  for (i = 0; i < tablinks.length; i++) {

    tablinks[i].className = tablinks[i].className.replace(" active", "");

  }

  document.getElementById(cityName).style.display = "block";

  evt.currentTarget.className += " active";

}

.tab {

  overflow: hidden;

  border: 1px solid #ccc;

  background-color: #f1f1f1;

}


.tab button {

  background-color: inherit;

  float: left;

  border: none;

  outline: none;

  cursor: pointer;

  padding: 14px 16px;

  transition: 0.3s;

  font-size: 17px;

}


.tab button:hover {

  background-color: #ddd;

}


.tab button.active {

  background-color: #ccc;

}


.tabcontent {

  display: none;

  padding: 6px 12px;

  border: 1px solid #ccc;

  border-top: none;

}

<div class="tab">

  <button class="tablinks" onclick="openDef(event, 'Sighthounds')">Sighthounds</button>

  <button class="tablinks" onclick="openDef(event, 'Origin')">Origin</button>

</div>


<div id="Sighthounds" class="tabcontent">

  <h3>Sighthounds</h3>

    <h3>The Sighthounds</h3>

      <p>

      By definition, a sighthound is simply a dog breed that hunts primarily by 

      sight and speed rather than by scent because of this they are sometimes 

      refered to as Gazehounds. Because sighthounds hunt by sight, they need to 

      have the speed and agility to keep up with their prey in order to keep 

      them in sight. Sighthound breeds typically have lean bodies, long legs, 

      and a flexible back – they also have deep chests to support their 

      unusually large hearts and lungs. To help you picture the typical 

      sighthound body, here is a list of breeds that are categorized as 

      sighthounds:


    <h4>Afghan Hound:</h4>

      <p>

      Wow very biscit stop it fren very hand that feed shibe heckin, many 

      pats. Shooberino h*ck blep shoob sub woofer very taste wow shoober, 

      snoot heckin mlem smol borking doggo with a long snoot for pats. Heck 

      I am bekom fat pupperino borkdrive length boy, such treat maximum 

      borkdrive wow very biscit. Borking doggo clouds much ruin diet 

      pupperino tungg super chub, very hand that feed shibe you are doin me 

      a concern boofers


      <span class="dots">...</span>

</div>


<div id="Origin" class="tabcontent">

  <h3>Origin</h3>

    <h5>Origin</h5>

      <p>

      Doggo ipsum smol borking doggo with a long snoot for pats bork mlem 

      heckin much ruin diet lotsa pats I am bekom fat, doing me a frighten most 

      angery pupper I have ever seen pats waggy wags vvv. big ol pupper big ol. 

      Ur givin me a spook smol borking doggo with a long snoot for pats big ol 

      pupper long bois ur givin me a spook bork smol adorable doggo, the 

      neighborhood pupper doing me a frighten puggorino tungg ur givin me a 

      spook. Blep the neighborhood pupper heckin good boys big ol pupper noodle 

      horse, doge big ol blep. noodle horse shibe maximum borkdrive. Bork you 

      are doing me a frighten boof pats, noodle horse extremely cuuuuuute you 

      are doing me the shock borkf, boof borkf.

     </p>


    <h4>Azwakah:</h4>

      <p>

      Wow very biscit stop it fren very hand that feed shibe heckin, many pats. 

      Shooberino h*ck blep shoob sub woofer very taste wow shoober, snoot 

      heckin mlem smol borking doggo with a long snoot for pats. Heck I am 

      bekom fat pupperino borkdrive length boy, such treat maximum borkdrive 

      wow very biscit. Borking doggo clouds much ruin diet pupperino tungg 

      super chub, very hand that feed shibe you are doin me a concern 

      boofers

      <span class="dots">...</span> 

</div>


查看完整回答
反對 回復 2022-08-04
  • 2 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號