javascript - 在 HTML 上单击图像时显示图像和文本
问题描述
在尝试让网站以下列方式做出响应时,我需要帮助:我希望能够单击图像,然后会在该图像旁边显示图像和一些信息。我已经实现了显示图像的部分,但不知道如何在它旁边显示文本。这是我到目前为止所拥有的:
<div class="image center">
<a href="images/123.jpg"><img src="images/123.jpg" alt="" /></a>
<p class="color names">123</p>
</div>
这基本上只是放大了屏幕上的图像。
解决方案
为了在单击图像时显示文本,您需要为其编写一个函数。
我在这里做了一个非常简单的例子,当点击图像时会出现一个文本,然后当你再次点击它时会消失。您可以根据需要修改代码。
HTML
<div class="image center">
<a href="#" onClick="showStuff()">
<img src="images/123.jpg" /></a>
<p class="color names">123</p>
</div>
# Here's the part that shows/hides when you click the image
<div id="hidden" style="display: none"><p>Here's the hidden text</p>
</div>
Javascript
function showStuff() {
let hidden = document.getElementById('hidden');
if (hidden.style.display == "none") {
hidden.style.display = "block"
} else {
hidden.style.display = "none"
}
}
推荐阅读
- vue.js - 登录后页面重定向
- python - 如何使用python中的预定义词组将字符串的单词分组为不同的字符串?
- python - 如何将外部变量传递到 lambda 函数的范围内?
- amazon-web-services - 在 PyCharm 中的何处配置我的 AWS“代码提交”凭证?[Elastic Beanstalk 代码管道]
- magento - Magento 2.4 目录搜索不适用于 2 和 3 字
- html - 如何使用angularJS在html中制作具有4个单元格的表格
- python - 如何修复 Python 文件中的某些“行太长”错误?
- git-submodules - git子模块与npm包?
- excel - 循环语句中的函数 IIF
- firebase - 侦听查询 FAILED_PRECONDITION 查询需要索引