简体   繁体   English

拉取标题并以列表格式显示在 div 中

[英]Pull the headings and display it in a div in a list format

I have a dynamic container .product_section and I need to display the headings inside to div.toc in a UL list format.我有一个动态容器.product_section ,我需要在div.toc中以 UL 列表格式显示标题。

Hoping to have a script that checks every .product_section and creates a list using jquery希望有一个脚本来检查每个.product_section并使用 jquery 创建一个列表

<div class="toc"></div>    

<div class="product_section">
  <h2>Heading 1</h2>
  <h3>Sub heading</h3>
  <h3>Sub heading</h3>
  <h3>Sub heading</h3>
</div>

<div class="product_section">
  <h2>Heading 2</h2>
  <h3>Sub heading</h3>
  <h3>Sub heading</h3>
  <h3>Sub heading</h3>
</div>

<div class="product_section">
  <h2>Heading 3</h2>
  <h3>Sub heading</h3>
  <h3>Sub heading</h3>
  <h3>Sub heading</h3>
</div>

How can I make this so that it will create a list in every .product_section我怎样才能做到这一点,以便它将在每个.product_section中创建一个列表

so it will be like this所以会是这样

<div class="toc">

<ul>
   <li><a href="#heading-1">Heading 1</a></li>
   <li><a href="#sub-heading">Sub heading</a></li>
   <li><a href="#sub-heading">Sub heading</a></li>
   <li><a href="#sub-heading">Sub heading</a></li>
</ul>

<ul>
   <li><a href="#heading-2">Heading 2</a></li>
   <li><a href="#sub-heading">Sub heading</a></li>
   <li><a href="#sub-heading">Sub heading</a></li>
   <li><a href="#sub-heading">Sub heading</a></li>
</ul>

<ul>
   <li><a href="#heading-3">Heading 3</a></li>
   <li><a href="#sub-heading">Sub heading</a></li>
   <li><a href="#sub-heading">Sub heading</a></li>
   <li><a href="#sub-heading">Sub heading</a></li>
</ul>

</div> 

You can try the following way:您可以尝试以下方法:

 //select all the elements with.product_section var products = document.querySelectorAll('.product_section'); //loop through them products.forEach(function(el){ //create ul element var ul = document.createElement('ul'); //find all h2, h3 from the currrent element var c = el.querySelectorAll('h2, h3'); //loop through them c.forEach(function(header){ //create li element var li = document.createElement('li'); //create anchor element var a = document.createElement('a'); //set the href attribute and set the link text a.href = '#' + header.textContent.toLowerCase().replace(' ', '-'); a.textContent = header.textContent; li.appendChild(a); //append the li to ul element ul.appendChild(li); }); //append the ul to div element document.querySelector('.toc').appendChild(ul); //remove the current div element el.remove(); });
 <div class="toc"></div> <div class="product_section"> <h2>Heading 1</h2> <h3>Sub heading</h3> <h3>Sub heading</h3> <h3>Sub heading</h3> </div> <div class="product_section"> <h2>Heading 2</h2> <h3>Sub heading</h3> <h3>Sub heading</h3> <h3>Sub heading</h3> </div> <div class="product_section"> <h2>Heading 3</h2> <h3>Sub heading</h3> <h3>Sub heading</h3> <h3>Sub heading</h3> </div>

Here it is in Jquery这是 Jquery

 var product_section_list = document.querySelectorAll('div.product_section'); product_section_list.forEach( function(product_section) { // create a new <ul> with class product_section var ul = $('<ul />').addClass("product_section"); product_section.querySelectorAll('h2, h3').forEach( function(h) { // create new <li> for each h2 and h3 returned ul.append($('<li />').append( // add a link anchor and set href and innerHTML $('<a />').attr('href',"#" +$(h).text().toLowerCase()).html($(h).text())) ); } ); //append ul into.toc element ul.appendTo(document.querySelector('.toc')); } );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="toc"></div> <div class="product_section"> <h2>Heading 1</h2> <h3>Sub heading</h3> <h3>Sub heading</h3> <h3>Sub heading</h3> </div> <div class="product_section"> <h2>Heading 2</h2> <h3>Sub heading</h3> <h3>Sub heading</h3> <h3>Sub heading</h3> </div> <div class="product_section"> <h2>Heading 3</h2> <h3>Sub heading</h3> <h3>Sub heading</h3> <h3>Sub heading</h3> </div>

PHP:格式化 html<div> 正确显示</div><div id="text_translate"><p>你好我想得到这个但是当我将我的 div 存储在我的数据库中并且当我尝试显示它时我无法正确显示它你能帮我吗?</p><p> 期望的结果:</p><pre> &lt;div data-tpl="header1" data-title="Header 1" class="drop-element"&gt;&lt;label&gt;LABEL 111&lt;/label&gt;&lt;/div&gt; &lt;div data-tpl="header2" data-title="Header 2" class="drop-element"&gt;&lt;input class="form-control" value="" id="changeInput" placeholder="INPUT 222"&gt;&lt;/div&gt;</pre><p> 在我的数据库中:</p><pre> &lt;div data-tpl=\"header1\" data-title=\"Header 1\" class=\"drop-element\"&gt;&lt;label&gt;LABEL 111&lt;/label&gt;&lt;/div&gt; &lt;div data-tpl=\"header2\" data-title=\"Header 2\" class=\"drop-element\"&gt;&lt;input class=\"form-control\" value=\"\" id=\"changeInput\" placeholder=\"INPUT 222\"&gt;&lt;/div&gt;</pre><p> 在我的页面上使用此 function 在 web 页面上显示。php 但显示不正确:</p><pre> preg_replace('/\\\\/', '', $formChaine);</pre><p> 我在 web 页面上有不需要的结果:</p><pre> &lt;div data-tpl="\&amp;quot;header1\&amp;quot;" data-title="\&amp;quot;Header" 1\"="" class="\&amp;quot;drop-element\&amp;quot;"&gt;&lt;label&gt;LABEL 111&lt;/label&gt;&lt;/div&gt; &lt;div data-tpl="\&amp;quot;header2\&amp;quot;" data-title="\&amp;quot;Header" 2\"="" class="\&amp;quot;drop-element\&amp;quot;"&gt;&lt;input class="\&amp;quot;form-control\&amp;quot;" value="\&amp;quot;\&amp;quot;" id="\&amp;quot;changeInput\&amp;quot;" placeholder="\&amp;quot;INPUT" 222\"=""&gt;&lt;/div&gt;</pre></div> - PHP : format an html <div> to display it correctly

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在div的Rails显示中拉出帖子图像 - Pull post image in rails display in div 如何从 HTML 中提取所有标题 textContents 并制作<ul>列出来?</ul> - How to pull all headings textContents out of HTML and make a <ul> list out of them? 使用Ajax在DIV中显示列表 - Display a List in a DIV with Ajax 将列表中的项目显示到 div 中 - Display items from list into a div PHP:格式化 html<div> 正确显示</div><div id="text_translate"><p>你好我想得到这个但是当我将我的 div 存储在我的数据库中并且当我尝试显示它时我无法正确显示它你能帮我吗?</p><p> 期望的结果:</p><pre> &lt;div data-tpl="header1" data-title="Header 1" class="drop-element"&gt;&lt;label&gt;LABEL 111&lt;/label&gt;&lt;/div&gt; &lt;div data-tpl="header2" data-title="Header 2" class="drop-element"&gt;&lt;input class="form-control" value="" id="changeInput" placeholder="INPUT 222"&gt;&lt;/div&gt;</pre><p> 在我的数据库中:</p><pre> &lt;div data-tpl=\"header1\" data-title=\"Header 1\" class=\"drop-element\"&gt;&lt;label&gt;LABEL 111&lt;/label&gt;&lt;/div&gt; &lt;div data-tpl=\"header2\" data-title=\"Header 2\" class=\"drop-element\"&gt;&lt;input class=\"form-control\" value=\"\" id=\"changeInput\" placeholder=\"INPUT 222\"&gt;&lt;/div&gt;</pre><p> 在我的页面上使用此 function 在 web 页面上显示。php 但显示不正确:</p><pre> preg_replace('/\\\\/', '', $formChaine);</pre><p> 我在 web 页面上有不需要的结果:</p><pre> &lt;div data-tpl="\&amp;quot;header1\&amp;quot;" data-title="\&amp;quot;Header" 1\"="" class="\&amp;quot;drop-element\&amp;quot;"&gt;&lt;label&gt;LABEL 111&lt;/label&gt;&lt;/div&gt; &lt;div data-tpl="\&amp;quot;header2\&amp;quot;" data-title="\&amp;quot;Header" 2\"="" class="\&amp;quot;drop-element\&amp;quot;"&gt;&lt;input class="\&amp;quot;form-control\&amp;quot;" value="\&amp;quot;\&amp;quot;" id="\&amp;quot;changeInput\&amp;quot;" placeholder="\&amp;quot;INPUT" 222\"=""&gt;&lt;/div&gt;</pre></div> - PHP : format an html <div> to display it correctly 将页面的所有标题转换为列表 - Converting all headings of a page into a list jQuery切换带标题的OL列表 - JQuery toggle a OL list with headings 排序列表中的标题排序问题 - Problem with ordering headings in a sorted list 我只想要那个 div 的标题 - I want only headings from that div 以下拉格式显示Sharepoint列表数据 - Display Sharepoint list data in Dropdown format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM