dom - JQuery going through a set of UL and dynamically set ids incremently on each one -


I have an unordered list containing many items called 'olist items', which are a square of ul but no id

OList.AppendFormat ("
  • "); OList.AppendFormat ("& lt; ul class = 'olistItems' & gt;"); OList.AppendFormat ("& lt; li>" + s.sName + "
  • "); OList.AppendFormat ("
  • " + s.eName + "& lt; / li & gt;"); OList.AppendFormat ("
  • " + s.Sdate + "& lt; / li & gt;"); OList.AppendFormat ("
  • " + s.EDate + "& lt; / li & gt;"); OList.AppendFormat ("& lt; / ul & gt;"); OList.AppendFormat ("& lt; / li>"); OList.AppendFormat ("& lt; / ul & gt;");

    I want to each. Once the OListItem class is retrieved, add an ID to it dynamically.

      var o = $ (". OListItem"); $ .eich (o, function) {var f = $ (this) .attr ("id", 'listItem' + i); I ++;});  

    To make sure about the approach, do I have it so far?

  • You can use this (from the element of the collection), note that id number does not start with Can:

      $ ('. OListItems'). Each (function (i) {$ (this) .attr ('id', 'listItem' + i);});  

    The, (index, element) , you already need in this case, the i index (zero-based) current You are on .oListItem .


    Comments