Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 === Using CSS selectors within a limited scope | |
| 2 | |
| 3 ==== Problem | |
| 4 | |
| 5 You want to find elements that are contained by a particular element. | |
| 6 | |
| 7 ==== Solution | |
| 8 | |
| 9 Call the `query()` or `queryAll()` methods on a DOM element. Invoking one of | |
| 10 these methods on an element restricts the scope of the query to the that | |
|
Kathy Walrath
2013/04/30 22:48:54
the that -> that
shailentuli
2013/05/06 08:33:21
Done.
| |
| 11 element's descendants: | |
| 12 | |
| 13 -------------------------------------------------------------------------------- | |
| 14 containerElement.query(cssSelector); | |
| 15 containerElement.queryAll(cssSelector); | |
| 16 -------------------------------------------------------------------------------- | |
| 17 | |
| 18 ==== Examples | |
| 19 | |
| 20 Consider the following table of user records: | |
| 21 | |
| 22 -------------------------------------------------------------------------------- | |
| 23 <table> | |
| 24 <tr><td>Jose</td><td class='status'>Accepted</td></tr> | |
| 25 <tr><td>Marie</td><td class='status'>Accepted</td></tr> | |
| 26 <tr><td>Kwame</td><td class='status'>Accepted</td></tr> | |
| 27 <tr><td>Rohan</td><td class='status'>Accepted</td></tr> | |
| 28 </table> | |
| 29 -------------------------------------------------------------------------------- | |
| 30 | |
| 31 The following code attaches an event handler to each <tr>. When a <tr> is | |
| 32 clicked, the text within the matching descendant <td> toggles: | |
| 33 | |
| 34 -------------------------------------------------------------------------------- | |
| 35 queryAll('tr').forEach((element) { | |
| 36 element.onClick.listen((event) { | |
| 37 var record = event.currentTarget.query('.status'); | |
| 38 record.innerHtml = record.innerHtml == 'Accepted' ? 'Declined' : 'Accepted'; | |
| 39 }); | |
| 40 }); | |
| 41 -------------------------------------------------------------------------------- | |
| 42 | |
| 43 Because the query is scoped to the just-clicked row, cells with the 'status' | |
| 44 class in in other rows are not affected. | |
|
Kathy Walrath
2013/04/30 22:48:54
in in -> in
shailentuli
2013/05/06 08:33:21
Done.
| |
| 45 | |
| 46 Note the use of `queryAll()` as a top-level function in the code above. Used | |
| 47 in this manner, `queryAll()` is scoped to the entire document. | |
| OLD | NEW |