@@ -123,16 +123,16 @@ import { Steps } from '@astrojs/starlight/components';
123123添加一个 ` <script> ` 标签提供客户端 JavaScript,以「监听」用户事件并做出相应的响应。
124124
125125<Steps >
126- 1 . 将以下 ` <script> ` 标签添加到 ` index.astro ` ,就在结束的 ` </body> ` 标签之前。
126+ 1 . 使用 Astro 内置的 TypeScript 支持, 将以下 ` <script> ` 标签添加到 ` index.astro ` ,就在结束的 ` </body> ` 标签之前。
127127
128128 ``` astro title="src/pages/index.astro" ins={2-9}
129129 <Footer />
130130 <script>
131131 const menu = document.querySelector('.menu');
132132
133- menu.addEventListener('click', () => {
133+ menu? .addEventListener('click', () => {
134134 const isExpanded = menu.getAttribute('aria-expanded') === 'true';
135- menu.setAttribute('aria-expanded', !isExpanded);
135+ menu.setAttribute('aria-expanded', `${ !isExpanded}` );
136136 });
137137 </script>
138138 </body>
@@ -151,9 +151,9 @@ import { Steps } from '@astrojs/starlight/components';
151151 ``` js title="src/scripts/menu.js"
152152 const menu = document .querySelector (' .menu' );
153153
154- menu .addEventListener (' click' , () => {
154+ menu? .addEventListener (' click' , () => {
155155 const isExpanded = menu .getAttribute (' aria-expanded' ) === ' true' ;
156- menu .setAttribute (' aria-expanded' , ! isExpanded);
156+ menu .setAttribute (' aria-expanded' , ` ${ ! isExpanded} ` );
157157 });
158158 ` ` `
159159
@@ -164,9 +164,9 @@ import { Steps } from '@astrojs/starlight/components';
164164 < script>
165165 const menu = document .querySelector (' .menu' );
166166
167- menu.addEventListener('click', () => {
167+ menu? .addEventListener (' click' , () => {
168168 const isExpanded = menu .getAttribute (' aria-expanded' ) === ' true' ;
169- menu.setAttribute('aria-expanded', !isExpanded);
169+ menu .setAttribute (' aria-expanded' , ` ${ ! isExpanded} ` );
170170 });
171171
172172 import " ../scripts/menu.js" ;
0 commit comments