简体   繁体   English

如何在单击div元素后显示更多文本?

[英]How can I display further text after a div element is clicked?

Currently I am working on a personal project. 目前我正在从事个人项目。 You can see the website I am making so far here: 你可以在这里看到我正在制作的网站:

https://codepen.io/JoyFulCoding/pen/EzWyKv https://codepen.io/JoyFulCoding/pen/EzWyKv

The problem is that I am struggling to add the following feature. 问题是我正在努力添加以下功能。 The feature is when a user clicks on any of the 6 colored boxes, a further information section should display like below: 该功能是当用户点击6个彩色框中的任何一个时,另一个信息部分应如下所示:

在此输入图像描述

I have tried adding a paragraph with an id that has it's display set to none initially. 我尝试添加一个id为最初显示设置为none的段落。 When the user clicks on one of the boxes, the corresponding text for that topic should be shown. 当用户单击其中一个框时,应显示该主题的相应文本。

HTML HTML

<p id="fbAdCampaignText> Example Text here </p>

CSS CSS

#fbAdCampaignText {
    display:none;
}

.display {
    display:inline-block;
}

jQuery jQuery的

 $("#fbAdCampaigns").on('click', function(){
     $("#fbAdCampaignText").toggleClass("display");
     });

Note: i am using display:none instead of visibility:hide because I don't want the hidden text to take up space since if it did, it may mess up the structure of each of the 6 boxes. 注意:我使用display:none而不是visibility:hide,因为我不希望隐藏文本占用空间,因为如果这样做,它可能会弄乱6个框中每个框的结构。

However, this code doesn't seem to do what I want, that is, show corresponding further information depending on which 1 of the 6 boxes is clicked. 但是,此代码似乎没有按照我想要的方式执行,即根据单击的6个框中的哪一个显示相应的进一步信息。 How can I display further text after an element is clicked in this manner? 在以这种方式单击元素后如何显示更多文本?

You meant this? 你的意思是? https://codepen.io/dravas/pen/NVgvgN?editors=1010 https://codepen.io/dravas/pen/NVgvgN?editors=1010

Just change 只是改变

$("#fbAdCampaignText").toggleClass("display")

to

$("#fbAdCampaignText").toggle()

And if you want to trigger only that div that is inside clicked element then: 如果你想只触发clicked元素内的div,那么:

$(".fbAdCampaigns").on('click', function(){
  $(this).find("#fbAdCampaignText").toggle();
});

Okay, a couple of things here. 好的,这里有几件事。 Firstly, you can't have more than 1 of the same id in your document, so if you have 6 of those paragraphs, lose the id and use a class instead. 首先,您的文档中不能有超过1个相同的ID,因此如果您有6个这样的段落,则丢失ID并使用类。

Using an id also gives you specificity issues, where the .display class would still have been overridden by the id's styles. 使用id也会给出特殊性问题,其中.display类仍然会被id的样式覆盖。

The other thing is that in your jQuery, you're going to toggle the class on all of those elements if you use a class. 另一件事是,在你的jQuery中,如果使用类,你将在所有这些元素上切换类。 You need to specifically toggle the class for the element you clicked, so use $(this) instead - to target the element you clicked. 您需要专门为您单击的元素切换类,因此请使用$(this)来定位您单击的元素。

HTML: HTML:

<p class="fbAdCampaignText> Example Text here </p>

CSS: CSS:

.fbAdCampaignText {
  display:none;
}

.fbAdCampaignText.display { // you would have had specificity issues with the id otherwise
    display:inline-block;
}

jQuery jQuery的

$(".fbAdCampaigns").on('click', function(){
  $(this).toggleClass("display"); // only target the clicked element
});

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

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