What does the .end() function do in jQuery?

后端 未结 5 1957
星月不相逢
星月不相逢 2020-12-08 04:23

I have already read this web page http://api.jquery.com/end/ but I am still clueless on what .end() actually does. What is it for and how do you use it?

I am also re

5条回答
  •  执念已碎
    2020-12-08 04:40

    It allows the current "scoping" to end and be re-defined. For example, lets say you have some HTML like:

    • A
    • B
    • C
    • D

    You could first select the parent by :

    $('#people')
    

    And modify the children ul elements like

    #('#people').find('ul').css('border', '1px solid #f00')
    

    But what happened if you wanted to continue to to edit the parent element (#people)? You could start a new finder $('#people') or just chain it to the first line, preceeded with an .end() to notify jQuery that you want to "close" the find() and scope the search back to the preceeding find (implicity the $('#people'), like so)

    #('#people').find('ul').css('border', '1px solid #f00').end().css('border', '1px dashed #00f')
    

    So that line would: grab all the child UL of #people, change their borders to red and then change the border of the parent #people element to dashed and blue.

提交回复
热议问题