1+ /*
2+ - get the third list item (hint: eq())
3+ - change its color to red
4+ - change the color of the rest of the list items to blue
5+ - *without doing another selection*, find the div.module and remove the class "module"
6+ */
7+
8+ $ ( document ) . ready ( function ( ) {
9+ var $li = $ ( 'li' ) . eq ( 2 ) ;
10+
11+ $li . css ( 'color' , 'red' )
12+ . siblings ( )
13+ . css ( 'color' , 'blue' ) ;
14+
15+ $li . parent ( ) . prev ( ) // div.module
16+ . removeClass ( 'module' ) ;
17+ } ) ;
18+
19+ /*
20+ - get the h1
21+ - store its text in a variable
22+ - use the stored text as the text for the first list item
23+ */
24+ $ ( document ) . ready ( function ( ) {
25+ $ ( 'li:first' ) . text ( $ ( 'h1' ) . text ( ) ) ;
26+ } ) ;
27+
28+ /*
29+ bonus points:
30+ - change the background color of *every other* table row (hint: use :odd or :even)
31+ */
32+ $ ( document ) . ready ( function ( ) {
33+ $ ( 'tr:odd' ) . css ( { 'backgroundColor' : '#ccc' } ) ;
34+ } ) ;
35+
36+ /*
37+ example showing find() and not()
38+ */
39+ $ ( document ) . ready ( function ( ) {
40+ $ ( 'ul' )
41+ . addClass ( 'rounded' )
42+ . find ( 'li' ) . not ( '.foo' )
43+ . css ( 'color' , 'blue' ) ;
44+ } ) ;
45+
46+ /*
47+ playing with list items
48+ */
49+ $ ( document ) . ready ( function ( ) {
50+ var $firstListItem = $ ( 'li:first' ) ;
51+
52+ $firstListItem
53+ . addClass ( 'current' )
54+ . siblings ( )
55+ . removeClass ( 'current' ) ;
56+
57+ var liColor = $ ( 'ul' )
58+ . css ( 'border' , '1px solid red' )
59+ . children ( )
60+ . css ( 'color' , 'blue' )
61+ . css ( 'color' ) ;
62+
63+ console . log ( liColor ) ; // 'blue'
64+ } ) ;
65+
66+ $ ( document ) . ready ( function ( ) {
67+ /* appending */
68+ $ ( 'li' )
69+ . each ( function ( i ) {
70+ var $li = $ ( this ) ;
71+ $ ( '<span/>' )
72+ . text ( ' number: ' + i )
73+ . appendTo ( $li ) ;
74+ } )
75+ . append ( '<span> hello</span>' ) ;
76+
77+ /* cloning and inserting */
78+ $ ( 'li' ) . each ( function ( ) {
79+ var $li = $ ( this ) ;
80+ var $newLi = $li . clone ( ) ;
81+
82+ $newLi . text ( 'new ' + $newLi . text ( ) ) ;
83+
84+ $newLi . insertAfter ( $li ) ;
85+ } ) ;
86+
87+ var $newUl = $ ( 'ul' ) . clone ( ) ;
88+
89+ $ ( '<div/>' )
90+ . insertAfter ( 'h1' )
91+ . append ( $newUl ) ;
92+
93+ } ) ;
94+
95+
96+ /*
97+ manipulation exercises
98+
99+ - add five new list items to the end of the unordered list (hint: for (i=0; i<5; i++) { ... } )
100+ */
101+
102+ $ ( document ) . ready ( function ( ) {
103+ var $ul = $ ( 'ul' ) ;
104+ var myListItems = [ ] ;
105+
106+ for ( var i = 0 ; i < 5 ; i ++ ) {
107+ // $ul.append('<li>this is my list item</li>');
108+ var text = 'this is my list item #' + i ;
109+ myListItems . push ( '<li>' + text + '</li>' ) ;
110+ }
111+
112+ var myNewHtml = myListItems . join ( '' ) ;
113+ $ul . append ( myNewHtml ) ;
114+ } ) ;
115+
116+
117+ /*
118+ - remove the odd list items (hint: .remove())
119+ */
120+ $ ( document ) . ready ( function ( ) {
121+ var $li = $ ( 'li:odd' ) . remove ( ) ;
122+ } ) ;
123+
124+ /*
125+ - add another h2 and another paragraph to div.module
126+ */
127+ $ ( document ) . ready ( function ( ) {
128+ var $module = $ ( 'div.module' ) ;
129+
130+ $module . append ( '<h2>new headline</h2>' ) ;
131+ $module . append ( '<p>new paragraph</p>' ) ;
132+ } ) ;
133+
134+
135+ $ ( document ) . ready ( function ( ) {
136+ /*
137+ - add a new div.module to the page after the existing one; put a copy of the existing unordered list inside it.
138+ */
139+ var $module = $ ( 'div.module' ) ;
140+ var $newUl = $ ( 'ul' ) . clone ( ) ;
141+
142+ var $newModule = $ ( '<div/>' )
143+ . addClass ( 'module' )
144+ . append ( $newUl )
145+ . insertAfter ( $module ) ;
146+
147+ /* make list items clickable; mark the current one */
148+ $ ( 'li' ) . click ( function ( ) {
149+ var $this = $ ( this ) ;
150+
151+ $this . siblings ( ) . removeClass ( 'current' ) ;
152+
153+ if ( $this . hasClass ( 'current' ) ) {
154+ $this . removeClass ( 'current' ) ;
155+ } else {
156+ $this . addClass ( 'current' ) ;
157+ }
158+ } ) ;
159+ } ) ;
160+
161+ $ ( document ) . ready ( function ( ) {
162+ /* toggle (event) */
163+ $ ( 'td' ) . toggle (
164+ function ( ) {
165+ var $td = $ ( this ) ;
166+ $td . addClass ( 'current' ) ;
167+ } ,
168+ function ( ) {
169+ var $td = $ ( this ) ;
170+ $td . removeClass ( 'current' ) ;
171+ }
172+ ) ;
173+
174+ /* mark a clicked <a>; log the href */
175+ $ ( 'a' ) . click ( function ( e ) {
176+ e . preventDefault ( ) ;
177+
178+ var $a = $ ( this ) ;
179+ $a . addClass ( 'clicked' ) ;
180+
181+ console . log ( $a . attr ( 'href' ) ) ;
182+ } ) ;
183+
184+ /* hover */
185+ $ ( 'li' ) . hover (
186+ function ( ) {
187+ $ ( this ) . addClass ( 'current' ) ;
188+ } ,
189+ function ( ) {
190+ $ ( this ) . removeClass ( 'current' ) ;
191+ }
192+ ) ;
193+
194+ /* click on p, simulate click on <a> */
195+ $ ( 'p:first' ) . click ( function ( e ) {
196+ var href = $ ( this ) . find ( 'a' ) . attr ( 'href' ) ;
197+ window . location . href = href ;
198+ } ) ;
199+
200+ /* timeouts and intervals */
201+ var myTimeout = setTimeout ( function ( ) {
202+ alert ( 'hi' ) ;
203+ } , 5000 ) ;
204+
205+ var myInterval = setInterval ( function ( ) {
206+ console . log ( 'hello' ) ;
207+ } , 1000 ) ;
208+
209+ $ ( 'p' ) . click ( function ( ) {
210+ clearTimeout ( myTimeout ) ;
211+ clearInterval ( myInterval ) ;
212+ } ) ;
213+
214+ /* animation */
215+ $ ( 'h1' ) . next ( 'p' ) . hide ( ) ;
216+
217+ $ ( 'h1' ) . click ( function ( ) {
218+ $ ( this ) . next ( 'p' ) . slideToggle ( ) ;
219+ } ) ;
220+
221+ $ ( 'div.module h2' ) . toggle (
222+ function ( ) {
223+ $ ( this ) . next ( ) . slideUp ( ) ;
224+ } ,
225+ function ( ) {
226+ $ ( this ) . next ( ) . slideDown ( ) ;
227+ }
228+ ) ;
229+
230+ $ ( 'div.module h2' ) . click ( function ( ) {
231+ $ ( this ) . next ( ) . slideToggle ( ) ;
232+ } ) ;
233+
234+ $ ( 'ul li' )
235+ . css ( 'position' , 'relative' )
236+ . toggle (
237+ function ( ) {
238+ $ ( this ) . animate ( {
239+ left : '20px'
240+ } , 500 ) ;
241+ } ,
242+ function ( ) {
243+ $ ( this ) . animate ( {
244+ left : '0px'
245+ } , 500 ) ;
246+ }
247+ ) ;
248+
249+ } ) ;
0 commit comments