|
| 1 | +<title>Simple classList manipulation</title> |
| 2 | +<style> |
| 3 | +#classListTest { |
| 4 | + padding: 5px; |
| 5 | + border: 1px solid #ccc; |
| 6 | + padding-bottom: 20px; |
| 7 | + position: relative; |
| 8 | +} |
| 9 | + |
| 10 | +#classListTest:after { |
| 11 | + content: 'class: ' attr(class); |
| 12 | + position: absolute; |
| 13 | + background: #c00; |
| 14 | + bottom: 0; |
| 15 | + right: 0; |
| 16 | + padding: 5px; |
| 17 | + color: #fff; |
| 18 | +} |
| 19 | + |
| 20 | +.big { font-size: 30px; line-height: 30px; } |
| 21 | +.bold { font-weight: bold; } |
| 22 | +.pink { background: #FF5E99; color: #fff; } |
| 23 | + |
| 24 | +#status { |
| 25 | + background: #c00; |
| 26 | +} |
| 27 | +</style> |
| 28 | +<article> |
| 29 | + <p>Clicking the buttons below will toggle the class on the <em>bacon ipsum</em> text below, assigning the class with the same name (styles seen below). This is done using the new <code>classList</code> API.</p> |
| 30 | + <p id="status">Not supported :(</p> |
| 31 | + <pre><code><style> |
| 32 | + .big { font-size: 30px; } |
| 33 | + .bold { font-weight: bold; } |
| 34 | + .pink { background: #FF5E99; color: #fff; } |
| 35 | +</style></code></pre> |
| 36 | + <p id="classListTest">Bacon ipsum dolor sit amet pancetta bresaola tenderloin, swine meatball tongue ham boudin t-bone ribeye jerky sausage. Pork loin cow shankle drumstick tri-tip, chicken venison strip steak.</p> |
| 37 | + <p id="toggleClass">Toggle a class: |
| 38 | + <input type="button" value="big" /> |
| 39 | + <input type="button" value="bold" /> |
| 40 | + <input type="button" value="pink" /> |
| 41 | + </p> |
| 42 | +</article> |
| 43 | +<script> |
| 44 | +// checkfor support |
| 45 | + |
| 46 | +var toggle = document.getElementById('toggleClass'), |
| 47 | + test = document.getElementById('classListTest'); |
| 48 | + |
| 49 | +if (toggleClass.classList) { |
| 50 | + var supported = document.getElementById('status'); |
| 51 | + supported.parentNode.removeChild(supported); |
| 52 | + // bit of event delegation otherwise we're binding to each input |
| 53 | + toggle.addEventListener('click', function (event) { |
| 54 | + if (event.target.nodeName == 'INPUT') { |
| 55 | + test.classList.toggle(event.target.value); |
| 56 | + } |
| 57 | + }, false); |
| 58 | +} else { |
| 59 | + // not supported |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +</script> |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
0 commit comments