简体   繁体   English

尝试使用CSS(活动)和JavaScript在点击时显示内容。 如何保持内容显示?

[英]Trying to display content on click using css (active) and javascript. How can I keep the content displaying?

I feel like this is incredibly simple but I'm just not quite getting it. 我觉得这非常简单,但我还不太了解。

What I'm trying to do: When you click on a circle image, text will show up below it and will stay there until another circle image is clicked on. 我正在尝试做的事情:当您单击一个圆形图像时,文本将显示在其下方,并将停留在该位置,直到单击另一个圆形图像为止。

This is what I have so far... https://codepen.io/elamen/pen/wYGwGw 这就是我到目前为止所拥有的... https://codepen.io/elamen/pen/wYGwGw

<div class="boxes">
<img src="http://www.conejousd.org/portals/0/Images/EnrollCVUSD/2017/school-logos/school-sm-aspen.png" style="width: 150px;">
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
</div>
<div class="boxes">
<img src="http://www.conejousd.org/portals/0/Images/EnrollCVUSD/2017/school-logos/school-sm-aspen.png" style="width: 150px;">
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
</div>
<div class="boxes">
<img src="http://www.conejousd.org/portals/0/Images/EnrollCVUSD/2017/school-logos/school-sm-aspen.png" style="width: 150px;">
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
<p>This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.</p>
</div>


.boxes {
opacity: 0.5;
float: left;
width: 200px;
height: 130px;
overflow: hidden;
}

.boxes:active {
opacity: 1;
height: auto;
}

$('.boxes > a').click(function() {
$('.boxes').removeClass('hovered');
$(this).parent().toggleClass('hovered');
});

I'm unsure why you used boxes:active in your CSS as the active box, so if you change that to '.hovered', it works as expected. 我不确定为什么您在CSS中使用box:active作为活动框,因此如果将其更改为“ .hovered”,它会按预期工作。 Also, I don't quite understand how your jQuery is working in the first place since the event listener is pointing to an 'a' element rather than an 'img'. 另外,由于事件监听器指向的是'a'元素而不是'img',因此我不太了解jQuery的工作方式。

In any case, here is a working pen for your reference. 无论如何,这是一支工作笔供您参考。

https://codepen.io/chrisaronchick/pen/MPyWgQ https://codepen.io/chrisaronchick/pen/MPyWgQ

Here is the CSS: 这是CSS:

.boxes {
  opacity: 0.5;
  float: left;
  width: 200px;
  height: 130px;
  overflow: hidden;
}

.hovered {
  opacity: 1;
  height: auto;
}

Here is the JavaScript: 这是JavaScript:

$('.boxes > img').click(function() {
  $('.boxes').removeClass('hovered');
  $(this).parent().toggleClass('hovered');
});

暂无
暂无

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

相关问题 使用Javascript在同一面板上显示不同的内容。 - Displaying different content on the same panel using Javascript. 即使我使用Java单击“确定”后,错误密码的警报也会继续显示。 如何停止连续显示警报消息 - The alert for wrong password keeps displaying, even after I click ok using Javascript. How do I stop this continuous display of the alert message 如何修改 CSS 标记内容? 不使用 JavaScript? - How can I modify a CSS tag content? Without using JavaScript? 所以我正在尝试使用 JavaScript 填充 HTML 表。 我不知道如何解除 arrays 以正确显示数据 - So I am trying to populate a HTML table using JavaScript. I can't figure out how to unshift the arrays to correctly display the data 如何使用JavaScript过滤内容 - How can i filter content using JavaScript CSS Javascript在用户点击时显示下一个/上一个Div的内容 - CSS Javascript to display content of next/previous Div on user click 如何在JavaScript的同一页面上显示文本内容? - How can I display text content on the same page in JavaScript? 尝试自动化jQuery click事件并显示内容 - Trying to automate jQuery click event and displaying content 我可以显示Javascript内容HTML - Can I display Javascript Content HTML 如何使用CSS(可能是样式)或javascript控制按钮的“值”或内容? - How can I control the “value” or content of a button using CSS (perhaps as a style) or through javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM