Wednesday, 15 May 2013

css - how to "chain" selector together and apply the same function in jQuery -


The title may not really tell what my question is.

  & lt; Ul & gt; & Lt; Li & gt; & Lt; / Li & gt; & Lt; Li & gt; B & lt; / Li & gt; & Lt; Li class = "test" & gt; C & lt; / Li & gt; & Lt; Li & gt; D & lt; / Li & gt; & Lt; / Ul & gt;   

What I am trying to do is apply the background color to the adjacent brothers with an element "exam", and with itself, three of them Background color will be why I have to do the following.

  $ ('ul li.test'). Css ('background', 'red'); $ (this). Css ('background', 'red'); $ (this) .next () CSS ('background', 'red');});   

Which is a way of doing something like this, what I think is a pretty ugly coding $ (back, next, next). Some functions () Thanks

To add an element to the set of matched elements, Add () :

  $ (this) .add ($ (this) .prev ()) .add ($ (this) .next () ) CSS ('background', 'red').   

or .addback () (for jQuery versions prior to 1.8 with .andSelf () ): >

  $ (this) .prev () addBack () next () addBack () CSS ('background', 'red') ....    

No comments:

Post a Comment