【问题标题】:Detect first ul element and change li inside or create new li with jquery .html()检测第一个 ul 元素并在内部更改 li 或使用 jquery .html() 创建新的 li
【发布时间】:2016-11-30 11:23:36
【问题描述】:

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

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

【问题讨论】:

  • 向我们展示您迄今为止的尝试?
  • 我只计算了元素并且真的不知道如何触发第一个第二个或第三个 ul 之后我用 .html() 更改了里面的 li。
  • 查看我的建议,希望对您有所帮助。

标签: javascript jquery html list find


【解决方案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

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多