[英]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.