簡體   English   中英

拉取標題並以列表格式顯示在 div 中

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

我有一個動態容器.product_section ,我需要在div.toc中以 UL 列表格式顯示標題。

希望有一個腳本來檢查每個.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>

我怎樣才能做到這一點,以便它將在每個.product_section中創建一個列表

所以會是這樣

<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> 

您可以嘗試以下方法:

 //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>

這是 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顯示中拉出帖子圖像 如何從 HTML 中提取所有標題 textContents 並制作<ul>列出來?</ul> 使用Ajax在DIV中顯示列表 將列表中的項目顯示到 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> 將頁面的所有標題轉換為列表 jQuery切換帶標題的OL列表 排序列表中的標題排序問題 我只想要那個 div 的標題 以下拉格式顯示Sharepoint列表數據
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM