简体   繁体   English

jQuery在li内迭代ul

[英]jquery iterate ul within li

I'm using the following code to iterate the HTML element given below 我正在使用以下代码来迭代下面给出的HTML元素

<ul id="sortable">
<li>
<p> item 1</p>
<ul id="nested-sortable">
  <li>
     Sub item 1 
  </li>
  <li>
     Sub item 2
  </li>
  <li>
     Sub item 3 
  </li>
</ul>
</li>
<li>
<p> item 2</p>
</li>
</ul>

Javascript Java脚本

var jsonSeqObj = {};
alert('hi');
$('ul#sortable li').each(function(index,pele) {
   alert($(pele).attr('id'));
   $(pele).children('ul#nested-sortable li').each(function(idx, chele) {
   alert(chele.attr('id'));
   jsonSeqObj.push({REF_ID:$(chele).attr('id') , NEW_SEQ_NO:idx });
});  
jsonSeqObj.push({REF_ID:$(pele).attr('id') , NEW_SEQ_NO:index });
});

li ul li is too long, line wraps but no indention li ul li太长,换行但没有缩进

alert(jsonSeqObj);
return jsonSeqObj;

but the children thing is not working 但是孩子们的东西不起作用

ERROR : Object doesnt support this property. 错误:对象不支持此属性。

Is your markup same as in the example? 您的标记与示例中的相同吗? Or you have more than one nested-sortable ul-s? 还是您有多个嵌套排序的ul? Maybe problem that you have duplicated IDs on the page (Id should be unique). 可能是您在页面上有重复的ID(ID应该是唯一的)的问题。

alert(chele.attr('id')); But chele here is DOM object (not jQuery), so it doesn't have attr method. 但是这里的chele是DOM对象(不是jQuery),因此它没有attr方法。 That's why you are getting 'object doesn't support this prperty...' 这就是为什么您得到“对象不支持此属性...”的原因

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM