title: Is there a way to automatically add classes in multi-level lists with jQuery?
body: <p>I have a multi-level lists like this:</p>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;Item 1
          &lt;ul&gt;
              &lt;li&gt;Item 1's 1st Child
                  &lt;ul&gt;
                      &lt;li&gt;Item 1's 1st Grandchild
                          &lt;ul&gt;
                              &lt;li&gt;Item 1's Grand Grandchild&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li&gt;Item 1's 2nd Grandchild&lt;/li&gt;
                      &lt;li&gt;Item 1's 3rd Grandchild&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;Item 1's 2nd Child&lt;/li&gt;
              &lt;li&gt;Item 1's 3rd Child&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Item 2&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>I want for each <code>li</code> to have class 'level' according to their positions. The result would be like this:</p>
  
  <pre><code>&lt;ul&gt;
      &lt;li class="level-1"&gt;Item 1
          &lt;ul&gt;
              &lt;li class="level-2"&gt;Item 1's 1st Child
                  &lt;ul&gt;
                      &lt;li class="level-3"&gt;Item 1's 1st Grandchild
                          &lt;ul&gt;
                              &lt;li class="level-4"&gt;Item 1's Grand Grandchild&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li class="level-3"&gt;Item 1's 2nd Grandchild&lt;/li&gt;
                      &lt;li class="level-3"&gt;Item 1's 3rd Grandchild&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li class="level-2"&gt;Item 1's 2nd Child&lt;/li&gt;
              &lt;li class="level-2"&gt;Item 1's 3rd Child&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li class="level-1"&gt;Item 2&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>Is there a way to achieve this with jQuery?</p>
  
  <p>Thank you very much for your attention.</p>