php 阅读更多切换按钮

jk9hmnmh  于 2023-02-11  发布在  PHP
关注(0)|答案(2)|浏览(123)

我有三个服务,与阅读更多按钮,我想他们都显示一些文本,当用户点击阅读更多按钮,它显示更多,但挣扎着让文本toogle.我正在使用一个CMS称为processwire.我有一些jquery,但不知道我会错吗?

$("#designLink").addClass("active");

$("#designLink").click(function (e) {
    e.preventDefault();
    $("#DesignService").show();
    $("#PrintService").hide();
    $("#PublishingService").hide();
    $("#DesignService").removeClass("visuallyhidden");
    $("#designLink").addClass("active");
    $("#printLink").removeClass("active");
    $("#publishingLink").removeClass("active");
});

$("#printLink").click(function (e) {
    e.preventDefault();
    $("#DesignService").hide();
    $("#PrintService").show();
    $("#PublishingService").hide();
    $("#PrintService").removeClass("visuallyhidden");
    $("#designLink").removeClass("active");
    $("#printLink").addClass("active");
    $("#publishingLink").removeClass("active");
});

$("#publishingLink").click(function (e) {
    e.preventDefault();
    $("#DesignService").hide();
    $("#PrintService").hide();
    $("#PublishingService").show();
    $("#PublishingService").removeClass("visuallyhidden");
    $("#designLink").removeClass("active");
    $("#printLink").removeClass("active");
    $("#publishingLink").addClass("active");
});
<div class="container--content">

      <div class="home-services cf">
        <h2 class="text-center">Our Services</h2>
        <div class="home-services__services-container">
          <div class="home-services__services grid flex">
            <div class="service-box lap-and-up-one-third grid__item">
<!-- design section -->
              <p class="service-box__link">Design</p>
              <img class="service-box__icon" src="<?php echo $config->urls->templates ?>public/images/icon-design.svg" onerror="this.onerror=null; this.src='icon-design.png'" alt="" title=" Design Work">
 <!--
   design text
          -->
              <?php echo "{$page->designServiceText}"; ?>

              <div class="toggle-box">
                <input type="checkbox" style="display: none" id="toggle-box__toggle">
                <label class="button" for="toggle-box__toggle">Toggle read more</label>
                <div><?php echo "{$page->designServiceTextMore}"; ?></div>
              </div>
            </div>
            <!--
printing section
            -->
            <div class="service-box lap-and-up-one-third grid__item">
              <p class="service-box__link">Print</p>
              <img class="service-box__icon" src="<?php echo $config->urls->templates ?>public/images/icon-print.svg" onerror="this.onerror=null; this.src='icon-print.png'" alt="" title="">
 <!--
print text
          -->
              <?php echo "{$page->printServiceText}"; ?>
              <div class="toggle-box">
                <input type="checkbox" style="display: none" id="toggle-box__toggle2">
                <label class="button" for="toggle-box__toggle2">Toggle read more</label>
                <div><?php echo "{$page->printServiceTextMore}"; ?></div>
              </div>
            </div>
            <!--
publishing section
          -->
            <div class="service-box lap-and-up-one-third grid__item">
              <p class="service-box__link">Publishing</p>
              <img class="service-box__icon" src="<?php echo $config->urls->templates ?>public/images/icon-publishing.svg" onerror="this.onerror=null; this.src='icon-publishing.png'" alt="" title=" Publishing Work">
 <!--
publishing text
          -->
              <?php echo "{$page->publishingServiceText}"; ?>
              <div class="toggle-box">
                <input type="checkbox" style="display: none" id="toggle-box__toggle3">
                <label class="button" for="toggle-box__toggle3">Toggle read more</label>
                <div><?php echo "{$page->publishingServiceTextMore}"; ?></div>
              </div>
            </div>
            <hr class="home-services__divider" />
          </div>
        </div>
      </div>

    </div>
soat7uwm

soat7uwm1#

您正在引用代码中不存在的类。publishingLink仅在Javascript中提及,而不在HTML或CSS中提及。

5hcedyr0

5hcedyr02#

这看起来像是<details>标记的一个很好的用例。

  • DOMContentLoaded上,获取所有<summary>元素。
  • 并将<span class="readmore">Read More...</span>附加到每个文件。
  • 然后在css中,将所有包含readmore类的元素设置为display:block(或除none之外的任何元素)
  • 并添加一个新的css规则,以隐藏任何具有open属性的<details>元素的readmore元素

唯一需要注意的是,如果您将<p>标记放入<summary>元素中,HTML将不会验证。

document.addEventListener('DOMContentLoaded', () => {
  const summaries = document.querySelectorAll('summary');
  const readmore = document.createElement('span');
  readmore.classList.add('readmore');
  readmore.innerText = 'Read More...';
  for (const summary of summaries) {
      summary.append(readmore.cloneNode(true));
    }
});
details > summary .readmore {
display:block;
}

details[open] > summary .readmore {
display: none;
}

summary > h2,
.readmore {
 font-weight: bold;
 cursor: pointer;
 }
 
.readmore:hover {
  text-decoration: underline;
}
<details>
  <summary>
  <h2>Article Title</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</summary>

<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</details>

相关问题