简体   繁体   中英

Converting all headings of a page into a list

Right now I'm struggling with some pretty basic jQuery (don't hate on me). My aim is to transform every h3 / h4 header of a page into a list to make a 'self-creating' sidebar navigation.

For example:

<h3>This is a header</h3>
    <h4>This is its subheader</h4>
       <p>Here's some explanation.<p>
    <h4>This is another subheader</h4>
       <p>Here's some explanation.<p>
<h3>Here is a new header</h3>
    <h4>With</h4>
       <p>Here's some explanation.<p>
    <h4>some</h4>
       <p>Here's some explanation.<p>
   <h4>other</h4>
       <p>Here's some explanation.<p>
   <h4>subheaders</h4>
       <p>Here's some explanation.<p>

Into:

<ul>
  <li>This is a header
    <ul>
      <li>This is its subheader</li>
      <li>This is another subheader</li>
    </ul>
  </li>
  <li>Here is a new header
    <ul>
      <li>With</li>
      <li>some</li>
      <li>new</li>
      <li>subheaders</li>
    </ul>
  </li>
</ul>

What I've done so far:

//Selecting all existing Headers and Subheaders of the content wrapper
var headers = $('.some-text-wrapper h3, .some-text-wrapper h4');

//Placing them into a new div (later sidebar)      
$('#headings').html(headers);

//Looping through each element trying to replace the tags
$('#headings h3, #headings h4').each(function(){
    if ( $(this).is(':first-child') ) {
        $(this).replaceWith('<ul><li>' + $(this).html() +'<ul>')
    } else if ( $(this).prop("tagName") == 'H3' ) {
        $(this).replaceWith('</ul></li><li>' + $(this).html() +'<ul>')
    } else {
        $(this).replaceWith('<li>' + $(this).html() +'</li>')
    }
});

Unfortunately I'm ending up with some mess like:

<ul>
   <li>This is a header
   <ul></ul>
   </li>
</ul>
<li>This is its subheader</li>

when it comes to the h3 headings so I haven't kept on working on the finishing tags etc. I don't know if the auto-closing of the ul/li tags is caused by the replacement in jQuery or by Wordpress (had some issues of autoclosing tags before).

I'm aware that I could simply use the created h3/h4 elements and style them as a list but that's not really what I am looking for. Anyway, I'm interested in finding a solution for this.

Thanks in advance.

You can use jquery's nextuntil ( https://api.jquery.com/nextUntil/ ) and pass it a selector and a filter to create the proper list.

 var output = ""; $("h3").each(function(){ output += "<li>" + $(this).text() + "</li>"; if($(this).next("h4").length > 0){ output += "<ul>"; $(this).nextUntil("h3","h4").each(function(){ output += "<li>" + $(this).text() + "</li>"; }); output += "</ul>"; } }); $("#list").html(output); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h3>This is a header</h3> <h4>This is its subheader</h4> <p>Here's some explanation.<p> <h4>This is another subheader</h4> <p>Here's some explanation.<p> <h3>Here is a new header</h3> <h4>With</h4> <p>Here's some explanation.<p> <h4>some</h4> <p>Here's some explanation.<p> <h4>other</h4> <p>Here's some explanation.<p> <h4>subheaders</h4> <p>Here's some explanation.<p> <div id="list"><ul></ul></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM