|
| 1 | +--- |
| 2 | +chapter : "js101" |
| 3 | +section: "2" |
| 4 | +title: "Operators" |
| 5 | +--- |
| 6 | +## Basic Operators |
| 7 | +Selecting Elements |
| 8 | + |
| 9 | +The most basic concept of jQuery is to “select some elements and do something with them.” |
| 10 | +jQuery supports most CSS3 selectors, as well as some non-standard selectors. |
| 11 | +For a complete selector reference, visit [http://api.jquery.com/category/selectors/](http://api.jquery.com/category/selectors/ "Selectors documentation on api.jquery.com"). |
| 12 | + |
| 13 | +Following are a few examples of common selection techniques. |
| 14 | + |
| 15 | +<div class="example" markdown="1"> |
| 16 | +Selecting elements by ID |
| 17 | + |
| 18 | + $('#myId'); // note IDs must be unique per page |
| 19 | +</div> |
| 20 | + |
| 21 | +<div class="example" markdown="1"> |
| 22 | +Selecting elements by class name |
| 23 | + |
| 24 | + $('div.myClass'); // performance improves if you specify element type |
| 25 | +</div> |
| 26 | + |
| 27 | +<div class="example" markdown="1"> |
| 28 | +Selecting elements by attribute |
| 29 | + |
| 30 | + $('input[name=first_name]'); // beware, this can be very slow |
| 31 | + |
| 32 | + |
| 33 | +<div class="example" markdown="1"> |
| 34 | +Selecting elements by compound CSS selector |
| 35 | + |
| 36 | + $('#contents ul.people li'); |
| 37 | + |
| 38 | +<div class="example" markdown="1"> |
| 39 | +Pseudo-selectors |
| 40 | + |
| 41 | + $('a.external:first'); |
| 42 | + $('tr:odd'); |
| 43 | + $('#myForm :input'); // select all input-like elements in a form |
| 44 | + $('div:visible'); |
| 45 | + $('div:gt(2)'); // all except the first three divs |
| 46 | + $('div:animated'); // all currently animated divs |
| 47 | +</div> |
| 48 | + |
| 49 | +### Note |
| 50 | + |
| 51 | +When you use the :visible and :hidden pseudo-selectors, jQuery tests the actual |
| 52 | +visibility of the element, not its CSS visibility or display — that is, it looks |
| 53 | +to see if the element's physical height and width on the page are both greater than zero. |
| 54 | +However, this test doesn't work with <tr> elements; in this case, jQuery does check |
| 55 | +the CSS display property, and considers an element hidden if its display property |
| 56 | +is set to none. Elements that have not been added to the DOM will always be |
| 57 | +considered hidden, even if the CSS that would affect them would render them |
| 58 | +visible. (See the Manipulation section later in this chapter to learn how to |
| 59 | +create and add elements to the DOM.) |
| 60 | + |
| 61 | +For reference, here is the code jQuery uses to determine whether an element is visible or hidden, with comments added for clarity: |
| 62 | + |
| 63 | +<div class="example" markdown="1"> |
| 64 | + jQuery.expr.filters.hidden = function( elem ) { |
| 65 | + var width = elem.offsetWidth, height = elem.offsetHeight, |
| 66 | + skip = elem.nodeName.toLowerCase() === "tr"; |
| 67 | + |
| 68 | + // does the element have 0 height, 0 width, |
| 69 | + // and it's not a <tr>? |
| 70 | + return width === 0 && height === 0 && !skip ? |
| 71 | + |
| 72 | + // then it must be hidden |
| 73 | + true : |
| 74 | + |
| 75 | + // but if it has width and height |
| 76 | + // and it's not a <tr> |
| 77 | + width > 0 && height > 0 && !skip ? |
| 78 | + |
| 79 | + // then it must be visible |
| 80 | + false : |
| 81 | + |
| 82 | + // if we get here, the element has width |
| 83 | + // and height, but it's also a <tr>, |
| 84 | + // so check its display property to |
| 85 | + // decide whether it's hidden |
| 86 | + jQuery.curCSS(elem, "display") === "none"; |
| 87 | + }; |
| 88 | + |
| 89 | + jQuery.expr.filters.visible = function( elem ) { |
| 90 | + return !jQuery.expr.filters.hidden( elem ); |
| 91 | + }; |
| 92 | +</div> |
| 93 | + |
| 94 | +### Choosing Selectors |
| 95 | + |
| 96 | +Choosing good selectors is one way to improve the performance of your JavaScript. |
| 97 | +A little specificity — for example, including an element type such as div when |
| 98 | + selecting elements by class name — can go a long way. Generally, any time you |
| 99 | +can give jQuery a hint about where it might expect to find what you're looking for, |
| 100 | +you should. On the other hand, too much specificity can be a bad thing. |
| 101 | +A selector such as #myTable thead tr th.special is overkill if a selector |
| 102 | +such as #myTable th.special will get you what you want. |
| 103 | + |
| 104 | +jQuery offers many attribute-based selectors, allowing you to make selections |
| 105 | +based on the content of arbitrary attributes using simplified regular expressions. |
| 106 | + |
| 107 | +<div class="example" markdown="1"> |
| 108 | + // find all <a>s whose rel attribute |
| 109 | + // ends with "thinger" |
| 110 | + $("a[rel$='thinger']"); |
| 111 | +</div> |
| 112 | + |
| 113 | +While these can be useful in a pinch, they can also be extremely slow — I once wrote an attribute-based selector that locked up my page for multiple seconds. Wherever possible, |
| 114 | +make your selections using IDs, class names, and tag names. |
| 115 | + |
| 116 | +Want to know more? Paul Irish has a great presentation about improving performance in |
| 117 | +JavaScript, with several slides focused specifically on selector performance. |
| 118 | + |
| 119 | +### Does My Selection Contain Any Elements? |
| 120 | + |
| 121 | +Once you've made a selection, you'll often want to know whether you have anything to work with. |
| 122 | + You may be inclined to try something like: |
| 123 | + |
| 124 | +<div class="example" markdown="1"> |
| 125 | +if ($('div.foo')) { ... } |
| 126 | +</div> |
| 127 | + |
| 128 | +This won't work. When you make a selection using `$()`, an object is always returned, |
| 129 | +and objects always evaluate to true. Even if your selection doesn't contain any elements, |
| 130 | +the code inside the if statement will still run. |
| 131 | + |
| 132 | +Instead, you need to test the selection's length property, which tells you how many |
| 133 | +elements were selected. If the answer is 0, the length property will evaluate to false |
| 134 | +when used as a boolean value. |
| 135 | + |
| 136 | +<div class="example" markdown="1"> |
| 137 | +Testing whether a selection contains elements |
| 138 | + |
| 139 | + if ($('div.foo').length) { ... } |
| 140 | +</div> |
| 141 | + |
| 142 | +### Saving Selections |
| 143 | + |
| 144 | +Every time you make a selection, a lot of code runs, and jQuery doesn't do caching of |
| 145 | +selections for you. If you've made a selection that you might need to make again, you |
| 146 | +should save the selection in a variable rather than making the selection repeatedly. |
| 147 | + |
| 148 | +<div class="example" markdown="1"> |
| 149 | +Storing selections in a variable |
| 150 | + |
| 151 | + var $divs = $('div'); |
| 152 | +</div> |
| 153 | + |
| 154 | +<div class="note" markdown="1"> |
| 155 | +### Note |
| 156 | + |
| 157 | +In “Storing selections in a variable”, the variable name begins with a dollar sign. |
| 158 | +Unlike in other languages, there's nothing special about the dollar sign in JavaScript — |
| 159 | +it's just another character. We use it here to indicate that the variable contains a |
| 160 | +jQuery object. This practice — a sort of Hungarian notation — is merely convention, |
| 161 | +and is not mandatory. |
| 162 | +</div> |
| 163 | + |
| 164 | +Once you've stored your selection, you can call jQuery methods on the variable you |
| 165 | +stored it in just like you would have called them on the original selection. |
| 166 | + |
| 167 | +<div class="note" markdown="1"> |
| 168 | +### Note |
| 169 | + |
| 170 | +A selection only fetches the elements that are on the page when you make the selection. |
| 171 | +If you add elements to the page later, you'll have to repeat the selection or otherwise |
| 172 | +add them to the selection stored in the variable. Stored selections don't magically |
| 173 | +update when the DOM changes. |
| 174 | +</div> |
| 175 | + |
| 176 | +### Refining & Filtering Selections |
| 177 | + |
| 178 | +Sometimes you have a selection that contains more than what you're after; in this case, you may want to refine your selection. jQuery offers several methods for zeroing in on exactly what you're after. |
| 179 | + |
| 180 | +<div class="example" markdown="1"> |
| 181 | +Refining selections |
| 182 | + |
| 183 | + $('div.foo').has('p'); // div.foo elements that contain <p>'s |
| 184 | + $('h1').not('.bar'); // h1 elements that don't have a class of bar |
| 185 | + $('ul li').filter('.current'); // unordered list items with class of current |
| 186 | + $('ul li').first(); // just the first unordered list item |
| 187 | + $('ul li').eq(5); // the sixth |
| 188 | +</div> |
| 189 | + |
| 190 | +### Selecting Form Elements |
| 191 | + |
| 192 | +jQuery offers several pseudo-selectors that help you find elements in your forms; |
| 193 | +these are especially helpful because it can be difficult to distinguish between |
| 194 | +form elements based on their state or type using standard CSS selectors. |
| 195 | + |
| 196 | +### :button |
| 197 | +Selects <button> elements and elements with type="button" |
| 198 | + |
| 199 | +#### :checkbox |
| 200 | +Selects inputs with type="checkbox" |
| 201 | + |
| 202 | +#### :checked |
| 203 | +Selects checked inputs |
| 204 | + |
| 205 | +#### :disabled |
| 206 | +Selects disabled form elements |
| 207 | + |
| 208 | +#### :enabled |
| 209 | +Selects enabled form elements |
| 210 | + |
| 211 | +#### :file |
| 212 | +Selects inputs with type="file" |
| 213 | + |
| 214 | +#### :image |
| 215 | +Selects inputs with type="image" |
| 216 | + |
| 217 | +#### :input |
| 218 | +Selects <input>, <textarea>, and <select> elements |
| 219 | + |
| 220 | +#### :password |
| 221 | +Selects inputs with type="password" |
| 222 | + |
| 223 | +#### :radio |
| 224 | +Selects inputs with type="radio" |
| 225 | + |
| 226 | +#### :reset |
| 227 | +Selects inputs with type="reset" |
| 228 | + |
| 229 | +#### :selected |
| 230 | +Selects options that are selected |
| 231 | + |
| 232 | +#### :submit |
| 233 | +Selects inputs with type="submit" |
| 234 | + |
| 235 | +#### :text |
| 236 | +Selects inputs with type="text" |
| 237 | + |
| 238 | +<div class="example" markdown="1"> |
| 239 | +Using form-related pseduo-selectors |
| 240 | + |
| 241 | + $('#myForm :input'); // get all elements that accept input |
| 242 | +</div> |
0 commit comments