modern-web-design-and-development

(Brent) #1

(^4)

  • Sub link 1.1

  • (^5)
  • Sub link 1.2

  • (^6)
  • Sub link 1.3

  • (^7)
    (^8)
  • Link 2
    (^9)

      (^10)
    • Sub link 2.1
      11
      (^12)
    • Sub link 2.2
      13
      (^14)

    (^15)

  • 16
    Imagine we wanted to color every third-generation
  • in that tree
    orange. Simple:
    1 $('#nav li').each(function() {
    (^2) if ($(this).parents('#nav li').length == 2)
    (^3) $(this).css('color', '#f90');
    4 });
    This translates like so: for every
  • found in #nav (hence our each()
    loop), whether it’s a direct child or not, see how many
  • parents/
    ancestors are above it within #nav. If the number is two, then this

  • must be on level three.


    closest(selector)


    This is a bit of a well-kept secret, but very useful. It works like parents(),
    except that it returns only one parent/ancestor. In my experience, you’ll
    normally want to check for the existence of one particular element in an

  • Free download pdf