首页 > 解决方案 > 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).

标签: javascripthtmlcss

解决方案


您可以尝试以下方法:

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>


推荐阅读