JS如何获取html中tagName的全部parents?

How to get the tagName's all parents in html by using JS?

有一个html,看起来像:

<html>
    <head></head>
    <body>
        <div>
            <h5 id="ca1">
                <span>text</span>
                <strong>text</strong>
                <label>text</label>
            </h5>
            <div>
                <h5 id = "ca2">

                </h5>
                <div id = "ca2body">
                    <table>
                        <tbody>
                            <tr>
                                <th><span>text</span></th>
                                <td>sth here</td>
                                <th><span>text</span></th>
                                <td>
                                  <label></label>
                                  text that i don't need
                                  <label></label>
                                  text that i don't need
                                  <label></label>
                                  text that i don't need
                                </td>
                                <script>            
                jQuery('#g2c4').change(function(){
                    if(jQuery('#g2c4').prop("checked")){ 
                        jQuery('#g2c4Span').show();
                        jQuery('.G2d').show();
                    }else{
                        jQuery('#g2c4Span').hide();
                        jQuery('.G2d').hide();                  

                        jQuery.each(jQuery('.chk_g2d'), function( index, item ) {                              
                            jQuery(item).prop('checked',false).change();
                        });
                    }                   
                });
            </script>
                            </tr>
                            <tr>
                                <th><span>text</span></th>
                                <td></td>
                                <th><span>text</span></th>
                                <td>
                                </td>
                            </tr>
                            <tr>TheSame</tr>
                            <tr>TheSame</tr>
                            <tr>TheSame</tr>
                        </tbody>
                    </table>
                </div>
                <h5 id = "ca3">

                </h5>
                <div id = "ca3body">
                    <table>
                        <tbody>
                            <tr>
                                <th><span>text</span></th>
                                <td></td>
                                <th><span>text</span></th>
                                <td></td>
                            </tr>
                            <tr>
                                <th><span>text</span></th>
                                <td></td>
                                <th><span>text</span></th>
                                <td></td>
                            </tr>
                            <tr>TheSame</tr>
                            <tr>TheSame</tr>
                            <tr>TheSame</tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </body>
</html>

我要获取文本:"sth here"所有parents标签名。 我需要让他们连接成一个词。 只是 like:html_body_div_table_tbody_tr_td

我该怎么办?

最后,我要制作一个Json文档

[
{
 "html_body_div_table_tbody_tr_th_span":"text",
 "html_body_div_table_tbody_tr_td":""
}
]

我会努力把它们做成字典, 并使用字典的键和键值来做。 够简单吗?

类似的东西?

let possible = document.querySelectorAll('body table tbody tr td');
let position = null;


possible.forEach(e=> { if(e.textContent=='sth here') position = e; })

if(position)
  {
  let response = position.tagName.toLowerCase();
  for(;;)
    {
    let pos = position.parentNode
      , tag = pos.tagName.toLowerCase()
      ;
    response = tag + '_' + response
    position = pos  
    if(tag=='html') break
    }
  console.log( "response -> " , response )
  }
else
  console.log('none!');
table { display:none;}
<table>
  <tbody>
    <tr>
      <th><span>text</span></th>
      <td>sth here</td>
      <th><span>text</span></th>
      <td></td>
    </tr>
    <tr>
      <th><span>text</span></th>
      <td></td>
      <th><span>text</span></th>
      <td></td>
    </tr>
    <tr><td>TheSame</td></tr>
    <tr><td>TheSame</td></tr>
    <tr><td>TheSame</td></tr>
  </tbody>
</table>

您可以使用parents()

const result = $('#ca2body tr').map(function() {
    var $children = $(this).find('*:not(:has(*))')
    var object = {}

    $children.each(function() {
        let key = $(this).parents().addBack().map((_, i) => i.tagName.toLowerCase()).get().join('_')
        let val = $(this).text()
        object[key] = val
    })

    return object
}).get()


console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div>
  <h5 id="ca1">
    <span>text</span>
    <strong>text</strong>
    <label>text</label>
  </h5>
  <div>
    <h5 id="ca2">

    </h5>
    <div id="ca2body">
      <table>
        <tbody>
          <tr>
            <th><span>text</span></th>
            <td>sth here</td>
            <th><span>text</span></th>
            <td></td>
          </tr>
          <tr>
            <th><span>text</span></th>
            <td></td>
            <th><span>text</span></th>
            <td></td>
          </tr>
          <tr>TheSame</tr>
          <tr>TheSame</tr>
          <tr>TheSame</tr>
        </tbody>
      </table>
    </div>
    <h5 id="ca3">

    </h5>
    <div id="ca3body">
      <table>
        <tbody>
          <tr>
            <th><span>text</span></th>
            <td></td>
            <th><span>text</span></th>
            <td></td>
          </tr>
          <tr>
            <th><span>text</span></th>
            <td></td>
            <th><span>text</span></th>
            <td></td>
          </tr>
          <tr>TheSame</tr>
          <tr>TheSame</tr>
          <tr>TheSame</tr>
        </tbody>
      </table>
    </div>
  </div>
</div>