简体   繁体   English

JavaScript 显示/隐藏(切换) function 切换类但不能在前端工作

[英]JavaScript Show / Hide (toggle) function toggling classes but not working at the frontEnd

My goal is to be able to show / hide excerpt of the content.我的目标是能够显示/隐藏内容的摘录。 This is a wordpress site so posts are generated hence giving a button a unique ID is not an option.这是一个 wordpress 站点,因此会生成帖子,因此不能选择为按钮提供唯一 ID。 It must be working for all added posts in the future too.将来它也必须适用于所有添加的帖子。

The excerpt should be first hidden and shown once the button is clicked.摘录应首先隐藏并在单击按钮后显示。

Current code adds and removes.excerpt class, however, the excerpt is not showing.当前代码添加和删除。摘录 class,但是,摘录没有显示。

HTML HTML

                <div class="card-img-top"><?php the_post_thumbnail(); ?></div>
                <div class="card-body">
                    <p class="card-text"><?php the_category(', '); ?></p>
                    <h5 class="card-title"><?php the_title(); ?></h5>
                    <p class="card-text excerpt"><?php the_excerpt(); ?></p>
                    <button class="btn btn-primary btn-md btn-rounded border-0 text-white read-more">Read More</button>
                </div>
                </div>

CSS CSS

.excerpt + p {
  display: none;
}


.excerpt {
  display: block;
}

JS JS

const readMoreBtns = document.querySelectorAll('.read-more')

const showMoreHandler = (event) => {
const cardActiveParagraphs = document.querySelectorAll('.excerpt + p')
  
const cardBody = event.target.parentElement
const cardParagraph = cardBody.querySelector('.excerpt + p')
  console.log(cardParagraph)

cardActiveParagraphs.forEach((paragraph) => {
  paragraph.classList.toggle('excerpt')
})
  
}

readMoreBtns.forEach((button) => 
{
button.addEventListener('click', e => showMoreHandler(e));
});

Setting display to none will render the page as though the element does not exist将 display 设置为 none 将呈现页面,就好像元素不存在一样

You can provide an ID to it and grab it with document.getElementById :您可以为其提供一个 ID 并使用document.getElementById获取它:

<p id="special-element" class="card-text"><?php the_category(', '); ?></p>

Then:然后:

const cardParagraph = document.getElementById('special-element')
// do what you need

Another thing you can do, is to have a 'hidden' class:您可以做的另一件事是拥有一个“隐藏的”class:

.hidden {
  display: none;
}

You don't need a class with display: block and display: none , just add / remove the hidden one.您不需要带有display: blockdisplay: none的 class ,只需添加/删除hidden的。

You can not toggle what is not in the dom.您无法切换 dom 中没有的内容。 When you try to run this line:当您尝试运行此行时:

const cardParagraph = cardBody.querySelector('.excerpt')
cardActiveParagraphs.forEach((paragraph) => {
      paragraph.classList.toggle('excerpt')
    })

You will only grab the elements that currently have the excerpt class, but not those that are turned off.您将仅获取当前具有摘录 class 的元素,而不是那些已关闭的元素。 A solution would be to add another class, just to grab them:一个解决方案是添加另一个 class,只是为了抓住它们:

<p class="card-text some-flag-class">the_category</p>
<h5 class="card-title">the_title</h5>
<p class="card-text excerpt some-flag-class">the_excerpt</p>

Then, in JS:然后,在 JS 中:

const cardActiveParagraphs = document.querySelectorAll('.some-flag-class');
cardActiveParagraphs.forEach((paragraph) => {
   paragraph.classList.toggle('excerpt')
})

This way, you will always grab both elements, regarding if they are on/off.这样,您将始终抓住这两个元素,无论它们是否打开/关闭。

Also, you don't need to have a display: block and display: none class.此外,您不需要display: blockdisplay: none class。 Enough to have:足够拥有:

.excerpt {
  display: none;
}

and toggle on/off that class.并打开/关闭 class。

I have solved the problem with the following js code:我用下面的js代码解决了这个问题:

 const readMoreBtns = document.querySelectorAll('.read-more') const showMoreHandler = (event) => { const cardBody = event.target.parentElement const cardActiveParagraphs = cardBody.querySelector('.excerpt + p') if(cardActiveParagraphs.style.display === 'none') { cardActiveParagraphs.style.display = 'block'; } else { cardActiveParagraphs.style.display = 'none'; } } readMoreBtns.forEach((button) => { button.addEventListener('click', e => showMoreHandler(e)); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM