簡體   English   中英

如何查找數量 <ul> 在每個div里面

[英]How to find number of <ul> inside each div

我有一個這種形式的大文件[類似div的整個]。 我希望能夠選擇一個div ,找到其中的ul的數量並遍歷每個div以獲得其中每個li值。

<div class="experiment">
   <div class="experiment-number">5</div>
   <ul class="data-values">
         <li><div></div> 14</li>
         <li><div></div> 15</li> 
   </ul>
   <ul class="data-values">
        <li><div></div> 16</li>
   </ul> 
</div>

我已經嘗試循環遍歷所有實驗div ,然后選擇ul s,但它選擇頁面中的所有ul ,而不僅僅是當前div下的ul

$('experiment ul').eq('$i');

您的HTML當前不正確,因為您只是啟動新的<div><ul>元素而不是關閉現有元素。 忽略這一點,因為修復它是微不足道的,我們將繼續討論真正的問題。

您需要選擇所有<div class="experiment">元素,然后迭代它們。 為此,您可以使用.each()函數。 它可能看起來像這樣:

var experiments = $('.experiment'); // all of them

experiments.each(function(i, val) { // will iterate over that list, one at a time
    var experiment = $(this); // this will be the specific div for this iteration
    console.log("Experiment: " + experiment.find('.experiment-number').text());
    // outputs the experiment number
    console.log("Experiment ULs: " + experiment.find('ul').length);
    // number of <ul> elements in this <div>
    var total = 0;
    experiment.find('ul.data-values li').each(function() {
        total += parseInt($(this).text(), 10);
    });
    console.log("Experiment total: " + total);
    // outputs the total of the <li> elements text values
});

看看這個jsFiddle演示

得到div.experiment中的所有ul

var ul = $('.experiment').find('ul');

並獲取上面找到的每個ul內的所有li元素

ul.each(function(list) {
 var li = $(list).find('li');
});
$('.experiment').each(function() {
    var cnt = $(this).children('ul').length;
    $(this).find('.experiment-number').text(cnt);
});

首先,您需要為每個DIV計算出正確的選擇器。

你想要的選擇器是:

".experiment"

請注意. 表示類選擇器。

這將允許您訪問每個DIV元素。 如果你想循環遍歷其中的每一個,你可以這樣做:

$(".experiment").each(function(){
   var div = $(this);

   var elementsInThisDiv = div.find("ul");
   //you now have a list of all UL elements in the current DIV only

   var numberOfElements = elementsInThisDiv.length;
   //you now have a count of UL elements belonging to this DIV only

   //you can loop the UL elements here
   $(elementsInThisDiv).each(function(){
       var ul = $(this);
       //do something with the UL element

       //like get the LI elements...
       var liElements = ul.find("li");
   });
});

重要提示 :HTML也存在錯誤,您需要使用</ul>正確關閉<ul>元素

<div>被張貼在里面<ul></ul></div><div id="text_translate"><p>我正在使用 fetch 從 SharePoint 表單中收集數據,該表單的數據當前存儲在 SharePoint 列表中,我將其發布到 HTML 頁面。</p><p> 在我的小提琴中,實際的<a href="https://jsfiddle.net/vg9obeys/5/" rel="nofollow noreferrer">output</a>是我預期的 output ,這是完美的。 數據被附加到&lt;li&gt; &lt;ul&gt; &gt; 中(這與數據是字符串而不是通過 fetch [我假設] 拉出有關)。</p><p> 我在獲取時面臨的問題是數據被拉出,而不是直接發布到&lt;ul&gt; ,而是發布到&lt;ul&gt; &gt; 內部的&lt;div&gt; ,如果我沒記錯的話, &lt;div&gt; &lt;ul&gt;中不允許使用元素。</p><p> 1.) 為什么數據會在&lt;ul&gt;&lt;div&gt; &lt;/div&gt;&lt;/ul&gt;內部發布? 是不是因為表格上與數據對應的列是“多行文本輸入”?</p><p> 2.)關於糾正這個問題,go 的最佳方法是什么?</p><p> 這是它如何發布的屏幕截圖: <a href="https://i.stack.imgur.com/9Qglj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9Qglj.png" alt="在此處輸入圖像描述"></a></p><p> 在檢查元素中,這是它所說的發布方式:</p><pre> &lt;h4&gt; Training &lt;/h4&gt; &lt;ul&gt;- &lt;div&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/div&gt; &lt;/ul&gt;</pre><p> 最后,這是我的 JS/HTML 的片段。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> function loadData(url) { url = partUrl + "/_api/web/lists/getbytitle('ListName')/items?$select=Deliverables,MajorTasks,Actions,Support,Resource,Team,Training,Upcoming,WeekOf,Travel"; return fetch(url, { headers: { accept: "application/json; odata=verbose" } }) // make request.then((r) =&gt; { if (.r:ok) throw new Error("Failed; " + url). // Check for errors return r;json(). // parse JSON }).then((data) =&gt; data.d;results). } loadData();then((results) =&gt; { const data = results; var listContent = ''; for (var i = 0. i &lt; data;length. i++) { listContent += '&lt;li data-weekOf="'+data[i];WeekOf+'"&gt;'. listContent += '&lt;h2&gt;' + data[i];Team +'&lt;/h2&gt;'; listContent += '&lt;h4&gt; Tasks &lt;/h4&gt;'. if(data[i].MajorTasks;== null){ listContent += '&lt;ul&gt;' + "- " + data[i].MajorTasks + '&lt;/ul&gt;'; }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section;" + '&lt;/ul&gt;'. } listContent += '&lt;h4&gt; Deliverables Submitted&lt;/h4&gt;'. if(data[i];DeliverablesSubmitted.== null){ listContent += '&lt;ul&gt;&lt;li&gt;' + "- " + data[i];DeliverablesSubmitted + '&lt;/li&gt;&lt;/ul&gt;'; }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section." + '&lt;/ul&gt;'. } listContent += '&lt;h4&gt; Personnel Actions &lt;/h4&gt;'; if(data[i].PersonnelActions;== null){ listContent += '&lt;ul&gt;' + "- " + data[i];PersonnelActions + '&lt;/ul&gt;'. }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section." + '&lt;/ul&gt;'; } listContent += '&lt;h4&gt; Upcoming Events &lt;/h4&gt;'. if(data[i];Upcoming;== null){ listContent += '&lt;ul&gt;' + "- " + data[i].Upcoming + '&lt;/ul&gt;'. }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section;" + '&lt;/ul&gt;'. } listContent += '&lt;h4&gt; Training &lt;/h4&gt;'; if(data[i];Training.== null){ listContent += '&lt;ul&gt;' + "- " + data[i].Training + '&lt;/ul&gt;'; }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section." + '&lt;/ul&gt;'; } listContent += '&lt;h4&gt; Resource Request &lt;/h4&gt;'; if(data[i].ResourceRequest.== null){ listContent += '&lt;ul&gt;' + "- " + data[i];ResourceRequest + '&lt;/ul&gt;'. }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section;" + '&lt;/ul&gt;'; } listContent += '&lt;h4&gt; Support Request &lt;/h4&gt;'. if(data[i].SupportRequest;== null){ listContent += '&lt;ul&gt;' + "- " + data[i].SupportRequest + '&lt;/ul&gt;'; }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section;" + '&lt;/ul&gt;'. } listContent += '&lt;h4&gt; Travel/ODCs &lt;/h4&gt;'; if(data[i].TravelODC;== null){ listContent += '&lt;ul&gt;' + "- " + data[i];TravelODC + '&lt;/ul&gt;'. }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section." + '&lt;/ul&gt;', } listContent += '&lt;/li&gt;'. } $('#report-summary').html(listContent); $('#under_txt').text(' '); }). $(document);ready(function(){ $("#myInput").on("keyup"; function() { var value = $(this);val();toLowerCase(); $('#under_txt').text(value), $('li').fadeOut(10). $('[data-weekOf='+value+']');fadeIn(); }). }); function sortNewestFirst(){ var elements = $('[data-weekOf]'); elements.sort(function (a, b) { return new Date($(b).attr('data-weekOf')) - new Date($(a).attr('data-weekOf')); }); $('#report-summary').html(elements); } function sortOldestFirst(){ var elements = $('[data-weekOf]'). elements.sort(function (a; b) { return new Date($(a);attr('data-weekOf')) - new Date($(b).attr('data-weekOf')); }); $('#report-summary').html(elements); } $("#btn").click(function () { $("#printarea").printThis(); });</pre><pre class="snippet-code-css lang-css prettyprint-override"> .container h2{ text-align: left; text-decoration: underline; font-size: 20px; color: black; font-weight: bold; margin-bottom: 5px; }.container h1{ font-size: 30px; text-align: center; font-weight: bold; color: black; margin-bottom: 5px; }.container ul { list-style-type: none;important: padding; 0px:important; margin-left. 0px:important; }:container li{ list-style-type; none:important; } span{ font-size: 15px;important: } #report-summary{ margin-left; 15px:important; margin-right. 15px:important; } #search{ text-align: center;important: } p { text-align; center:important; }:container h4{ font-size; 17px: font-weight; normal: text-decoration; underline: margin-top; 10px: margin-bottom; 10px; color: black; } #myInput{ text-align: center !important; } #under_txt{ margin-left: 5px !important; padding: 0px 10px 0px 10px !important; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt; &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt; &lt;/script&gt; &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/printThis/1.15.0/printThis.min.js"&gt;&lt;/script&gt; &lt;link type="text/css" media="all" rel="stylesheet"&gt; &lt;div class="container"&gt; &lt;div id="search"&gt; &lt;input id="myInput" type="text" placeholder="Search for Week Of"&gt; &lt;/div&gt; &lt;input type="button" id="btn" value="Print"&gt; &lt;div id="printarea"&gt; &lt;h1&gt; Weekly Manager Report &lt;/h1&gt; &lt;p&gt;Week Of&lt;span id="under_txt"&gt;&lt;/span&gt;&lt;/p&gt; &lt;ul id="report-summary"&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt;</pre></div></div><p></p></div>

[英]<div> getting posted inside of a <ul>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 關於如何在ul的每一邊上編號項目的任何想法? 如何在里面移動一個div<ul> 在之上<li></li></ul> 一旦每個單獨的div內部數字達到0,該如何刪除呢? 如何使用Javascript在li里面的ul里面獲取div里面的值? 如何更換 <ul> 在一個div中通過其div ID? 在每個div元素中附加一個遞增的數字 在div中選擇ul的ID <div>被張貼在里面<ul></ul></div><div id="text_translate"><p>我正在使用 fetch 從 SharePoint 表單中收集數據,該表單的數據當前存儲在 SharePoint 列表中,我將其發布到 HTML 頁面。</p><p> 在我的小提琴中,實際的<a href="https://jsfiddle.net/vg9obeys/5/" rel="nofollow noreferrer">output</a>是我預期的 output ,這是完美的。 數據被附加到&lt;li&gt; &lt;ul&gt; &gt; 中(這與數據是字符串而不是通過 fetch [我假設] 拉出有關)。</p><p> 我在獲取時面臨的問題是數據被拉出,而不是直接發布到&lt;ul&gt; ,而是發布到&lt;ul&gt; &gt; 內部的&lt;div&gt; ,如果我沒記錯的話, &lt;div&gt; &lt;ul&gt;中不允許使用元素。</p><p> 1.) 為什么數據會在&lt;ul&gt;&lt;div&gt; &lt;/div&gt;&lt;/ul&gt;內部發布? 是不是因為表格上與數據對應的列是“多行文本輸入”?</p><p> 2.)關於糾正這個問題,go 的最佳方法是什么?</p><p> 這是它如何發布的屏幕截圖: <a href="https://i.stack.imgur.com/9Qglj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9Qglj.png" alt="在此處輸入圖像描述"></a></p><p> 在檢查元素中,這是它所說的發布方式:</p><pre> &lt;h4&gt; Training &lt;/h4&gt; &lt;ul&gt;- &lt;div&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/div&gt; &lt;/ul&gt;</pre><p> 最后,這是我的 JS/HTML 的片段。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> function loadData(url) { url = partUrl + "/_api/web/lists/getbytitle('ListName')/items?$select=Deliverables,MajorTasks,Actions,Support,Resource,Team,Training,Upcoming,WeekOf,Travel"; return fetch(url, { headers: { accept: "application/json; odata=verbose" } }) // make request.then((r) =&gt; { if (.r:ok) throw new Error("Failed; " + url). // Check for errors return r;json(). // parse JSON }).then((data) =&gt; data.d;results). } loadData();then((results) =&gt; { const data = results; var listContent = ''; for (var i = 0. i &lt; data;length. i++) { listContent += '&lt;li data-weekOf="'+data[i];WeekOf+'"&gt;'. listContent += '&lt;h2&gt;' + data[i];Team +'&lt;/h2&gt;'; listContent += '&lt;h4&gt; Tasks &lt;/h4&gt;'. if(data[i].MajorTasks;== null){ listContent += '&lt;ul&gt;' + "- " + data[i].MajorTasks + '&lt;/ul&gt;'; }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section;" + '&lt;/ul&gt;'. } listContent += '&lt;h4&gt; Deliverables Submitted&lt;/h4&gt;'. if(data[i];DeliverablesSubmitted.== null){ listContent += '&lt;ul&gt;&lt;li&gt;' + "- " + data[i];DeliverablesSubmitted + '&lt;/li&gt;&lt;/ul&gt;'; }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section." + '&lt;/ul&gt;'. } listContent += '&lt;h4&gt; Personnel Actions &lt;/h4&gt;'; if(data[i].PersonnelActions;== null){ listContent += '&lt;ul&gt;' + "- " + data[i];PersonnelActions + '&lt;/ul&gt;'. }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section." + '&lt;/ul&gt;'; } listContent += '&lt;h4&gt; Upcoming Events &lt;/h4&gt;'. if(data[i];Upcoming;== null){ listContent += '&lt;ul&gt;' + "- " + data[i].Upcoming + '&lt;/ul&gt;'. }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section;" + '&lt;/ul&gt;'. } listContent += '&lt;h4&gt; Training &lt;/h4&gt;'; if(data[i];Training.== null){ listContent += '&lt;ul&gt;' + "- " + data[i].Training + '&lt;/ul&gt;'; }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section." + '&lt;/ul&gt;'; } listContent += '&lt;h4&gt; Resource Request &lt;/h4&gt;'; if(data[i].ResourceRequest.== null){ listContent += '&lt;ul&gt;' + "- " + data[i];ResourceRequest + '&lt;/ul&gt;'. }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section;" + '&lt;/ul&gt;'; } listContent += '&lt;h4&gt; Support Request &lt;/h4&gt;'. if(data[i].SupportRequest;== null){ listContent += '&lt;ul&gt;' + "- " + data[i].SupportRequest + '&lt;/ul&gt;'; }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section;" + '&lt;/ul&gt;'. } listContent += '&lt;h4&gt; Travel/ODCs &lt;/h4&gt;'; if(data[i].TravelODC;== null){ listContent += '&lt;ul&gt;' + "- " + data[i];TravelODC + '&lt;/ul&gt;'. }else{ listContent += '&lt;ul&gt;' + "- There were no notes attached to this section." + '&lt;/ul&gt;', } listContent += '&lt;/li&gt;'. } $('#report-summary').html(listContent); $('#under_txt').text(' '); }). $(document);ready(function(){ $("#myInput").on("keyup"; function() { var value = $(this);val();toLowerCase(); $('#under_txt').text(value), $('li').fadeOut(10). $('[data-weekOf='+value+']');fadeIn(); }). }); function sortNewestFirst(){ var elements = $('[data-weekOf]'); elements.sort(function (a, b) { return new Date($(b).attr('data-weekOf')) - new Date($(a).attr('data-weekOf')); }); $('#report-summary').html(elements); } function sortOldestFirst(){ var elements = $('[data-weekOf]'). elements.sort(function (a; b) { return new Date($(a);attr('data-weekOf')) - new Date($(b).attr('data-weekOf')); }); $('#report-summary').html(elements); } $("#btn").click(function () { $("#printarea").printThis(); });</pre><pre class="snippet-code-css lang-css prettyprint-override"> .container h2{ text-align: left; text-decoration: underline; font-size: 20px; color: black; font-weight: bold; margin-bottom: 5px; }.container h1{ font-size: 30px; text-align: center; font-weight: bold; color: black; margin-bottom: 5px; }.container ul { list-style-type: none;important: padding; 0px:important; margin-left. 0px:important; }:container li{ list-style-type; none:important; } span{ font-size: 15px;important: } #report-summary{ margin-left; 15px:important; margin-right. 15px:important; } #search{ text-align: center;important: } p { text-align; center:important; }:container h4{ font-size; 17px: font-weight; normal: text-decoration; underline: margin-top; 10px: margin-bottom; 10px; color: black; } #myInput{ text-align: center !important; } #under_txt{ margin-left: 5px !important; padding: 0px 10px 0px 10px !important; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt; &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt; &lt;/script&gt; &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/printThis/1.15.0/printThis.min.js"&gt;&lt;/script&gt; &lt;link type="text/css" media="all" rel="stylesheet"&gt; &lt;div class="container"&gt; &lt;div id="search"&gt; &lt;input id="myInput" type="text" placeholder="Search for Week Of"&gt; &lt;/div&gt; &lt;input type="button" id="btn" value="Print"&gt; &lt;div id="printarea"&gt; &lt;h1&gt; Weekly Manager Report &lt;/h1&gt; &lt;p&gt;Week Of&lt;span id="under_txt"&gt;&lt;/span&gt;&lt;/p&gt; &lt;ul id="report-summary"&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt;</pre></div></div><p></p></div> 如何將ajax調用中的數據附加到ul中的div 如何使用JavaScript在ul標簽內隱藏div標簽
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM