|
44 | 44 | </div>
|
45 | 45 | </div>
|
46 | 46 |
|
| 47 | + <style> |
| 48 | + .es-switcher > p.active, .es-switcher > p:hover { |
| 49 | + color: #FFF; |
| 50 | + background-color: rgb(51, 51, 51); |
| 51 | + border-color: rgb(51, 51, 51); |
| 52 | + } |
| 53 | + .es-switcher > p { |
| 54 | + display: inline-block; |
| 55 | + margin: 0px; |
| 56 | + cursor: pointer; |
| 57 | + padding: 0em 21px; |
| 58 | + border-style: solid; |
| 59 | + border-color: rgb(255, 255, 255); |
| 60 | + border-width: 3px; |
| 61 | + background-color: rgb(238, 238, 238); |
| 62 | + border-top-right-radius: 3px; |
| 63 | + border-top-left-radius: 3px; |
| 64 | + } |
| 65 | + .es-switcher > pre { |
| 66 | + border-top-left-radius: 0px !important; |
| 67 | + margin: 0px; |
| 68 | + } |
| 69 | + </style> |
| 70 | + |
| 71 | + <script> |
| 72 | +var esNextText = 'Modern JavaScript:' |
| 73 | +var es5Text = 'ES5:' |
| 74 | +
|
| 75 | +function nextSiblingMatch (e, name, text) { |
| 76 | + var sib = e.nextSibling |
| 77 | + while (sib) { |
| 78 | + if (sib.nodeType == 1) { |
| 79 | + if (sib.nodeName == name && (!text || sib.textContent == text)) |
| 80 | + return sib |
| 81 | + return null |
| 82 | + } |
| 83 | + sib = sib.nextSibling |
| 84 | + } |
| 85 | +} |
| 86 | +
|
| 87 | +Array.prototype.slice.call(document.querySelectorAll('p')) |
| 88 | + .filter(function (p) { return p.textContent == esNextText }) |
| 89 | + .map(function (esNextP) { |
| 90 | + var esNextPre = nextSiblingMatch(esNextP, 'PRE') |
| 91 | + if (!esNextPre) return null |
| 92 | + var es5P = nextSiblingMatch(esNextPre, 'P') |
| 93 | + if (!es5P) return null |
| 94 | + var es5Pre = nextSiblingMatch(es5P, 'PRE') |
| 95 | + if (!es5Pre) return null |
| 96 | + return { esNextP: esNextP, esNextPre: esNextPre, es5P: es5P, es5Pre: es5Pre } |
| 97 | + }) |
| 98 | + .filter(Boolean) |
| 99 | + .forEach(function (block) { |
| 100 | + var div = document.createElement('div') |
| 101 | + div.className = 'es-switcher' |
| 102 | + block.esNextP.parentElement.insertBefore(div, block.esNextP) |
| 103 | + block.esNextP.textContent = esNextText.replace(/:$/, '') |
| 104 | + block.es5P.textContent = es5Text.replace(/:$/, '') |
| 105 | + block.esNextP.className = 'active' |
| 106 | + div.appendChild(block.esNextP) |
| 107 | + div.appendChild(block.es5P) |
| 108 | + div.appendChild(block.esNextPre) |
| 109 | + div.appendChild(block.es5Pre) |
| 110 | + block.es5Pre.style.display = 'none' |
| 111 | + block.esNextP.addEventListener('click', function () { |
| 112 | + block.esNextPre.style.display = 'block' |
| 113 | + block.es5Pre.style.display = 'none' |
| 114 | + block.esNextP.className = 'active' |
| 115 | + block.es5P.className = '' |
| 116 | + }) |
| 117 | + block.es5P.addEventListener('click', function () { |
| 118 | + block.esNextPre.style.display = 'none' |
| 119 | + block.es5Pre.style.display = 'block' |
| 120 | + block.esNextP.className = '' |
| 121 | + block.es5P.className = 'active' |
| 122 | + }) |
| 123 | + }) |
| 124 | + </script> |
47 | 125 | {{> footer }}
|
48 | 126 | </body>
|
49 | 127 | </html>
|
0 commit comments