简体   繁体   English

如何查找数量 <ul> 在每个div里面

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

I have a large file of this form [similar div's throughout]. 我有一个这种形式的大文件[类似div的整个]。 I want to be able to select a div , find the number of ul 's in it and traverse through each of them to get value of each li in it. 我希望能够选择一个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>

I have tried looping through all experiment div s, then select the ul s, but it selects all the ul in the page, not only the ones under current div . 我已经尝试循环遍历所有实验div ,然后选择ul s,但它选择页面中的所有ul ,而不仅仅是当前div下的ul

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

Your HTML is currently incorrect, since you're simply starting new <div> and <ul> elements rather than closing the existing ones. 您的HTML当前不正确,因为您只是启动新的<div><ul>元素而不是关闭现有元素。 Ignoring that because it's trivial to fix, we'll move on to the real issue. 忽略这一点,因为修复它是微不足道的,我们将继续讨论真正的问题。

You need to select all of the <div class="experiment"> elements, then iterate through them. 您需要选择所有<div class="experiment">元素,然后迭代它们。 To do that you can use the .each() function. 为此,您可以使用.each()函数。 It might look something like this: 它可能看起来像这样:

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
});

Take a look at this jsFiddle demo . 看看这个jsFiddle演示

to get all the ul inside div.experiment 得到div.experiment中的所有ul

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

and to get all li elements inside each ul found above 并获取上面找到的每个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);
});

First of all you need to work out the correct selector for each DIV. 首先,您需要为每个DIV计算出正确的选择器。

The selector you want is: 你想要的选择器是:

".experiment"

Notice the . 请注意. to denote a class selector. 表示类选择器。

This will allow you access to each DIV element. 这将允许您访问每个DIV元素。 If you then want to loop though each of these, you can do so like this: 如果你想循环遍历其中的每一个,你可以这样做:

$(".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");
   });
});

IMPORTANT : There is also an error with your HTML, you need to close your <ul> elements correctly using </ul> 重要提示 :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的每一边上编号项目的任何想法? - Any ideas on how to number each side of a ul with items inside of it? 如何在里面移动一个div<ul> 在之上<li></li></ul> - How to move a div inside <ul> on top of <li> 一旦每个单独的div内部数字达到0,该如何删除呢? - How to remove each indivicual div once number inside it reaches 0? 如何使用Javascript在li里面的ul里面获取div里面的值? - How to get value inside div inside ul inside li with Javascript? 如何更换 <ul> 在一个div中通过其div ID? - How to replace a <ul> inside a div by its div id? 在每个div元素中附加一个递增的数字 - appending an incrementing number inside each div element 在div中选择ul的ID - Select id of ul inside div <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> 如何将ajax调用中的数据附加到ul中的div - How to append data from ajax call to a div inside ul 如何使用JavaScript在ul标签内隐藏div标签 - how to hide div tags inside ul tag using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM