检测第一个ul元素并更改内部li或使用jquery创建新的li .html()

时间:2016-11-30 11:23:37

标签: javascript jquery html list find

我需要知道如何检测特定div或表单中的第一个,第二个或第三个ul元素。我正在尝试做的是,一旦在页面上检测到ul,我想更改第二个ul元素中的li

var ulcountfood = $('#nl-form').find('ul').length;
        alert(ulcountfood);
        //Change second ul li List html

1 个答案:

答案 0 :(得分:1)

您应该尝试使用.eq()来确定当前ul是第一个还是第二个等等。

请参阅文档:https://api.jquery.com/eq/

因此,对于每个ul,您需要检查其索引,

$('#nl-form').find('ul').each(function(){

   if($(this).eq(0)) {
     //do your first ul specific logic
   } // and so on for other ul

});