javascript - Add figcaption name to h2
问题描述
I need to add chosen figcaption to h2. Here is a code
<div id="custom_form">
<h2>Cars</h2>
<div class="models">
<div class="sections" onclick="buyclick1()">
<img src="2.png" width="200px" ;>
<figcaption>GX</figcaption>
</div>
<div class="sections" onclick="buyclick1()">
<img src=" 3.png " width="200px ">
<figcaption>GT</figcaption>
</div>
</div>
</div>
How can I add figcaption name to h2, consider that it shoud depends on what person click on? It should look like in online shop where you toggle between categories and subcategories of a product (# Cars>GT>Another categorie).
解决方案
您可以尝试以下方法:
var t = document.querySelector('h2').textContent;
function buyclick1(el){
var h2El = document.querySelector('h2');
h2El.textContent = t;
var caption = el.children[1].textContent;
h2El.textContent += ' >> ' + caption;
}
<div id="custom_form">
<h2>Cars</h2>
<div class="models">
<div class="sections" onclick="buyclick1(this)">
<img src="2.png" width="200px";>
<figcaption>GX</figcaption>
</div>
<div class="sections" onclick="buyclick1(this)">
<img src="3.png" width="200px">
<figcaption>GT</figcaption>
</div>
</div>
</div>
推荐阅读
- strapi - 在 Strapi 中添加虚拟属性
- python-3.x - 对于 python 价格查找 (webscraper) 项目,是否可以从用户那里获取输入并找到确切的产品
- linux - 从共享库调用函数是如何工作的?
- python - 如何使用 Numpy 编写程序来生成和打印 0 和 1 之间的 5 个随机数
- reactjs - React 原生道具管理
- javascript - 如何通过 jquery 向 HTML 表添加单选按钮
- mysql - 需要将 MySQL 查询转换为 PostgreSQL
- grafana - Grafana - 图表 - 1 条图表线可以有超过 1 种颜色吗?
- c - 如何编写一个从文件描述符中读取并返回一行的函数,而 C 中没有'\n'
- c - 共享内存读取修改数组