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

标签 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

最佳答案

您应该尝试使用 .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

});

关于javascript - 检测第一个 ul 元素并在内部更改 li 或使用 jquery .html() 创建新的 li,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40887105/

相关文章:

javascript - 可重复使用的 JavaScript 切换函数

html - 删除边框时 div 会缩小

javascript - Mocha - 柴 karma "suite is not defined"

jquery - Google PageSpeed Insights 结果 jQuery 减慢加载速度

javascript - 在 PHP 中重写 JSON 数组以便 jQuery 可以读取它?

javascript - lightbox2 图片库根本不起作用

javascript (window.open) 在所有浏览器中打开最大化,但在 chrome 中不打开

javascript - 是否可以更精细地控制 jQuery UI Dialog Widget 的显示/隐藏方法?

javascript - HTML Doctype 设置/IE Quirks 模式

javascript - CSS:当 image1 动画不正确时,图像集会移动,而它们应该留在原处