简体   繁体   English

当我尝试对它们进行排序时,如何在javascript中获取当前ul li的长度?

[英]How to get the current ul li's length in javascript , when i tried to sort them?

<ul id="types" style="list-type:none;">

<li id="categories" style="list-type:none;">1
<ul>
    <li style="list-type:none;">1</li><br/>
    <li style="list-type:none;">1</li>
</ul>
</li>

<li id="categories" style="list-type:none;">2
<ul>
    <li style="list-type:none;">2</li><br/>
    <li style="list-type:none;">2</li>
</ul>
</li>

<li id="categories" style="list-type:none;">3
<ul>
    <li style="list-type:none;">3</li><br/>
    <li style="list-type:none;">3</li>
</ul>
</li>

</ul>

i am moving the li up and down ,when i am doing like this i have written the code for calling one jquery _mouseStop function(event,noPropagation)) 我在上下移动li时,像这样,我已经编写了调用一个jquery _mouseStop函数的代码(event,noPropagation)

in this function i am writing 我在写这个函数

var tagetNode = this.currentItem[0].parentNode.id

if (this.element[0].id == "categories") {
    var targetlistlength = document.getElementById(targetListId).getElementsByTagName("li").length;
    // var targetlistlength = this.parentNode.getElementsByTagName("li").length;
    var PageIds = "";
    for (var i = 0; i < targetlistlength; i++) {
        PageIds += document.getElementById(targetListId).getElementsByTagName("li")[i].getAttribute("value") + ",";
    }

}

when i am sorting the child li's the above function get called but it is always getting the first child ul of first li length only.. i want the current ul li's length which i am trying to sort 当我对子li进行排序时,上面的函数被调用,但是它总是只获取第一个li长度的第一个子ul。我想要我要排序的当前ul li的长度

i have tried with the commented line also to get the length of li's but iam getting error as this.parentNode is undefined 我也尝试过用注释行来获取li的长度,但是由于this.parentNode是未定义的,我会出错

anyone please suggest me what can i do 任何人都可以建议我该怎么办

As there is no working demo available, you can check what you get when you use console.log($(this)); 由于没有可用的演示程序,因此您可以检查使用console.log($(this)); inside that function when mouseStop event occurs in console. 在控制台中发生mouseStop事件时,该函数内部。 If you get the expected li in the console then you can executing the following code to get the length of its parent's childs. 如果您在控制台中得到了预期的li ,则可以执行以下代码来获取其父级的子级的长度。

alert($(this).parent().children().length);

There mustn't be multiple elements with the same id. 不得有多个具有相同ID的元素。 Try to replace them with classes or make them unique. 尝试将它们替换为类或使其唯一。

The if-condition could be the following if you have unique id's (eg categories-1, categories-2, ...) 如果您具有唯一的ID,则if条件可以为以下条件(例如,category-1,category-2,...)

if (this.element[0].id.match("^categories") != null) { ... }

Or if you use classes the if-condition could be: 或者,如果您使用类,则if条件可以是:

if (this.element[0].className.match("(^| )categories( |$)") != null) { ... }

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

相关问题 怎么排序 <ul><li> 基于javascript的课程? - How to sort <ul><li>'s based on class with javascript? 如何获取所有LI UL元素ID值并将它们放置在JavaScript数组中? - How do I get all the LI UL elements ID value and place them in a JavaScript array? 如何排序<li>里面的元素<ul>使用 JavaScript(冒泡排序)? - How to sort <li> elements inside <ul> with JavaScript (bubble sort)? $(&#39;。photos ul li&#39;)。length在我注入照片之前输出0,当它们出现时输出5,但是当我删除它们时仍然显示5 - $('.photos ul li').length outputs 0 before i inject the photos and outputs five when they appear but when i remove them it still shows 5 如何使用Javascript获取ul li值 - How to get the ul li values using Javascript 如何在特定“ul”元素下获取多个“li”元素并将它们放入数组中? - How would I get multiple "li" elements under a specific "ul" element and put them into an array? 如何通过LI的data-title参数对UL列表进行排序? - How to sort UL list by LI's data-title parameter? 获取 ul li javascript 的事件 - get event of ul li javascript 在JavaScript中获取ul并附加li - get ul in javascript and append li 如何在使用jquery或javascript进行悬停时显示此ul li下拉菜单? - How do I get this ul li dropdown menu to show on hover with jquery or javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM