使用选择器或方法进行 JQuery 列表遍历帮助

发布于 2024-08-31 17:12:32 字数 1172 浏览 1 评论 0原文

我基本上在遍历无序列表和检索列表项时遇到了一些麻烦。

  foreach (MyTypeObject s in result)
        {

            oList.Clear();

             {


            oList.AppendFormat("<ul id='OuteroListItems'>");
            oList.AppendFormat("<li>");
            oList.AppendFormat("<ul id='oListItems'>");
            oList.AppendFormat("<li>" + s.Name + "</li>");
            oList.AppendFormat("<li>" + s.NameDesc + "</li>");
            oList.AppendFormat("<li>" + s.StartDate + "</li>");
            oList.AppendFormat("<li>" + s.EndDate + "</li>");
            oList.AppendFormat("</ul>");
            oList.AppendFormat("</li>");
            oList.AppendFormat("</ul>");

            sb.Append(oList);


        }

好的,我基本上有一个无序列表中的项目列表,然后是一个无序列表,其中包含一个包含项目本身的项目列表。

对于其中的每一个,我都试图选择开始日期

,所以说我在“OuteroListItems”中有 3 个无序列表,我想选择所有 3 个 s.StartDates 并将它们在“oListItems”中设置为红色。

我已经尝试过这个,但它只选择外部列表中的第一个元素第三个内部列表元素并将其着色为红色。

   $("ul#OuteroListItems li").each(function(){

    $("ul#oListItems li:eq(2)").css("color", "red");

    });

I'm basically having a bit of trouble with traversing through an unordered list and retreiving list items.

  foreach (MyTypeObject s in result)
        {

            oList.Clear();

             {


            oList.AppendFormat("<ul id='OuteroListItems'>");
            oList.AppendFormat("<li>");
            oList.AppendFormat("<ul id='oListItems'>");
            oList.AppendFormat("<li>" + s.Name + "</li>");
            oList.AppendFormat("<li>" + s.NameDesc + "</li>");
            oList.AppendFormat("<li>" + s.StartDate + "</li>");
            oList.AppendFormat("<li>" + s.EndDate + "</li>");
            oList.AppendFormat("</ul>");
            oList.AppendFormat("</li>");
            oList.AppendFormat("</ul>");

            sb.Append(oList);


        }

ok, I basically have a list of items in one unordered list and then an unordered list holding a list of items which hold items initself.

For each one of these I am trying to select the start date

so say I had 3 unordered lists inside of 'OuteroListItems', i would want to select all 3 of these s.StartDates and color them red in 'oListItems'.

I've tried this but it only select the first element in the outer lists 3rd inner list element and coloring that red.

   $("ul#OuteroListItems li").each(function(){

    $("ul#oListItems li:eq(2)").css("color", "red");

    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮凉 2024-09-07 17:12:32

首先,您需要使用类而不是 ID :) ID必须是唯一的,否则你会得到各种奇怪的行为...当它们不唯一时,它是无效的 HTML,只需将代码中的 id= 更改为 class = 来解决这个问题。您的输出现在应如下所示:

<ul class='OuteroListItems'>
  <li>
    <ul class='oListItems'>
      <li>s.Name</li>
      <li>s.NameDesc</li>
      <li>s.StartDate</li>
      <li>s.EndDate</li>
    </ul>
  </li>
</ul>

然后您可以使用以下选择器来获取每个 StartDate

  • $(".oListItems li:nth-child(3)").css("color", "red");​
    

    您可以在此处查看一个工作示例

    First you need to use class instead of ID :) IDs have to be unique or you'll get all sorts of funky behavior...when they're not unique it's invalid HTML, just change id= in your code to class= to fix this. Your output should now look like this:

    <ul class='OuteroListItems'>
      <li>
        <ul class='oListItems'>
          <li>s.Name</li>
          <li>s.NameDesc</li>
          <li>s.StartDate</li>
          <li>s.EndDate</li>
        </ul>
      </li>
    </ul>
    

    Then you can use the following selector to get each StartDate <li>:

    $(".oListItems li:nth-child(3)").css("color", "red");​
    

    You can see a working example here

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文