diff options
Diffstat (limited to 'files/zh-cn/web')
75 files changed, 2117 insertions, 1329 deletions
diff --git a/files/zh-cn/web/accessibility/aria/aria_techniques/index.html b/files/zh-cn/web/accessibility/aria/aria_techniques/index.html index 2f704dff7f..e8117f3603 100644 --- a/files/zh-cn/web/accessibility/aria/aria_techniques/index.html +++ b/files/zh-cn/web/accessibility/aria/aria_techniques/index.html @@ -1,136 +1,214 @@ --- -title: ARIA techniques +title: '使用 ARIA:角色、状态和属性' slug: Web/Accessibility/ARIA/ARIA_Techniques tags: - - ARIA - - Accessibility - - NeedsTranslation - - TopicStub +- ARIA +- 可访问性 +- 无障碍 +- Overview +- Reference translation_of: Web/Accessibility/ARIA/ARIA_Techniques --- -<h3 id="Roles">Roles</h3> - -<h4 id="Widget_roles">Widget roles</h4> - -<ul> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_alert_role" title="Using the alert role">Alert</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_alertdialog_role" title="Using the alertdialog role">Alertdialog</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role" title="en/ARIA/ARIA_Techniques/Using_the_button_role">Button</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_checkbox_role" title="en/ARIA/ARIA_Techniques/Using_the_checkbox_role">Checkbox</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_dialog_role" title="Using the dialog role">Dialog</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_link_role" title="en/ARIA/ARIA_Techniques/Using_the_link_role">Link</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_log_role" title="en/ARIA/ARIA_Techniques/Using_the_log_role">Log</a></li> - <li>Marquee</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_progressbar_role" title="en/ARIA/ARIA_Techniques/Using_the_progressbar_role">Progressbar</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_radio_role" title="en/ARIA/ARIA_Techniques/Using_the_radio_role">Radio</a></li> - <li>Scrollbar</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_slider_role" title="en/ARIA/ARIA_Techniques/Using_the_slider_role">Slider</a></li> - <li>Spinbutton</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_status_role" title="Using the link role">status</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_textbox_role" title="en/ARIA/ARIA_Techniques/Using_the_textbox_role">textbox</a></li> - <li>Timer</li> - <li>Tooltip</li> -</ul> - -<h4 id="Composite_roles">Composite roles</h4> - -<p>The techniques below describes each composite role as well as their required and optional child roles.</p> - -<ul> - <li>Grid (including row, gridcell, rowheader, columnheader roles)</li> - <li>Menubar/ Menu (including menuitem, menuitemcheckbox, menuitemradio)</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_listbox_role" title="en/ARIA/ARIA_Techniques/Using_the_listbox_role">Listbox</a> (包含 option role)</li> - <li>Tablist (包含 tab 和 tabpanel roles)</li> - <li>Tree (including group and treeitem roles)</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_radio_role" title="en/ARIA/ARIA_Techniques/Using_the_radio_role">Radiogroup (see radio role)</a></li> - <li>Treegrid</li> -</ul> - -<h4 id="Document_structure_roles">Document structure roles</h4> - -<ul> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_article_role" title="en/Accessibility/ARIA/ARIA_Techniques/Using_the_article_role">Article</a></li> - <li>Definition</li> - <li>Directory</li> - <li>Document</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_group_role" title="en/ARIA/ARIA_Techniques/Using_the_group_role">Group</a></li> - <li>Heading</li> - <li>Img</li> - <li>List, listitem</li> - <li>Math</li> - <li>Note</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_presentation_role" title="en/ARIA/ARIA_Techniques/Using_the_presentation_role">Presentation</a></li> - <li>Region</li> - <li>Separator</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_toolbar_role" title="en/ARIA/ARIA_Techniques/Using_the_toolbar_role">Toolbar</a></li> -</ul> - -<h4 id="Landmark_roles">Landmark roles</h4> - -<ul> - <li>Application</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_banner_role" title="en/ARIA/ARIA_Techniques/Using_the_banner_role">Banner</a></li> - <li>Complementary</li> - <li>Contentinfo</li> - <li>Form</li> - <li>Main</li> - <li>Navigation</li> - <li>Search</li> -</ul> - -<h3 id="States_and_properties">States and properties</h3> - -<h4 id="Widget_attributes">Widget attributes</h4> - -<ul> - <li>aria-autocomplete</li> - <li>aria-checked</li> - <li>aria-disabled</li> - <li>aria-expanded</li> - <li>aria-haspopup</li> - <li>aria-hidden</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-invalid_attribute" title="Using the aria-invalid attribute">aria-invalid</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-label_attribute" title="Using the aria-labelledby attribute">aria-label</a></li> - <li>aria-level</li> - <li>aria-multiline</li> - <li>aria-multiselectable</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-orientation_attribute" title="en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-orientation_attribute">aria-orientation</a></li> - <li>aria-pressed</li> - <li>aria-readonly</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-required_attribute" title="Using the aria-required property">aria-required</a></li> - <li>aria-selected</li> - <li>aria-sort</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-valuemax_attribute" title="Using the aria-required attribute">aria-valuemax</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-valuemin_attribute" title="en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-valuemin_attribute">aria-valuemin</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-valuenow_attribute" title="en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-valuenow_attribute">aria-valuenow</a></li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-valuetext_attribute" title="en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-valuetext_attribute">aria-valuetext</a></li> -</ul> - -<h4 id="Live_region_attributes">Live region attributes</h4> - -<ul> - <li>aria-live</li> - <li>aria-relevant</li> - <li>aria-atomic</li> - <li>aria-busy</li> -</ul> - -<h4 id="Drag_drop_attributes">Drag & drop attributes</h4> - -<ul> - <li>aria-dropeffect</li> - <li>aria-dragged</li> -</ul> - -<h4 id="Relationship_attributes">Relationship attributes</h4> - -<ul> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-activedescendant_attribute" title="Role">aria-activedescendant</a></li> - <li>aria-controls</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-describedby_attribute" title="Using the aria-labelledby attribute">aria-describedby</a></li> - <li>aria-flowto</li> - <li><a href="/en/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-labelledby_attribute" title="en/ARIA/ARIA_Techniques/Using_the_aria-labelledby_attribute">aria-labelledby</a></li> - <li>aria-owns</li> - <li>aria-posinset</li> - <li>aria-setsize</li> -</ul> + +<p>ARIA 定义了可以应用于元素的语义,这些语义分为 <strong>roles</strong>(定义一种用户界面元素)、<strong>states</strong> 和 <strong>properties</strong>,这些都是角色所支持的。作者必须在元素的生命周期中为其分配一个 ARIA 角色和适当的状态和属性,除非该元素已经具有适当的 ARIA 语义(通过使用适当的 HTML 元素)。添加 ARIA 语义只会向浏览器的可访问性 API 公开额外信息,而不会影响页面的 DOM。</p> + +<h2 id="Roles">角色</h2> + +<h3 id="Widget_roles">组件角色</h3> + +<div class="index"> + <ul> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/button_role">button</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/checkbox_role">checkbox</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Gridcell_role">gridcell</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_link_role">link</a></li> + <li>menuitem</li> + <li>menuitemcheckbox</li> + <li>menuitemradio</li> + <li>option</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_progressbar_role">progressbar</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_radio_role">radio</a></li> + <li>scrollbar</li> + <li>searchbox</li> + <li>separator (when focusable)</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_slider_role">slider</a></li> + <li>spinbutton</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Switch_role">switch</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Tab_Role">tab</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Tabpanel_Role">tabpanel</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/textbox_role">textbox</a></li> + <li>treeitem</li> + </ul> +</div> + +<h3 id="Composite_roles">复合角色</h3> + +<p>下面的技术描述了每个复合角色及其必需和可选的子角色。</p> + +<div class="index"> + <ul> + <li>combobox</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Grid_Role">grid</a> (包含 <a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Row_Role">row</a>, <a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Gridcell_role">gridcell</a>, rowheader, columnheader)</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/listbox_role">listbox</a> (包含 option)</li> + <li>menu</li> + <li>menubar</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_radio_role">radiogroup</a> (参考 radio)</li> + <li>tablist (包含 <a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Tab_Role">tab</a> 和 <a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Tabpanel_Role">tabpanel</a>)</li> + <li>tree</li> + <li>treegrid</li> + </ul> +</div> + +<h3 id="Document_structure_roles">文档结构角色</h3> + +<div class="index"> + <ul> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Application_Role">application</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Article_Role">article</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Cell_Role">cell</a></li> + <li>columnheader</li> + <li>definition</li> + <li>directory</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Document_Role">document</a></li> + <li>feed</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Figure_Role">figure</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_group_role">group</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/heading_role">heading</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Role_Img">img</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/List_role">list</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Listitem_role">listitem</a></li> + <li>math</li> + <li>none</li> + <li>note</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_presentation_role">presentation</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Row_Role">row</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Rowgroup_Role">rowgroup</a></li> + <li>rowheader</li> + <li>separator</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Table_Role">table</a></li> + <li>term</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_toolbar_role">toolbar</a></li> + <li>tooltip</li> + </ul> +</div> + +<h3 id="Landmark_roles">具有里程碑意义的角色</h3> + +<div class="index"> + <ul> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Banner_role">banner</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Complementary_role">complementary</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Contentinfo_role">contentinfo</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Form_Role">form</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Main_role">main</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Navigation_Role">navigation</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Region_role">region</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Search_role">search</a></li> + </ul> +</div> + +<h3 id="Live_Region_Roles">实时区域角色</h3> + +<div class="index"> + <ul> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Alert_Role">alert</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_log_role">log</a></li> + <li>marquee</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_status_role">status</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/ARIA_timer_role">timer</a></li> + </ul> +</div> + +<h3 id="Window_Roles">窗口角色</h3> + +<div class="index"> + <ul> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_alertdialog_role">alertdialog</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/dialog_role">dialog</a></li> + </ul> +</div> + +<h2 id="States_and_properties">状态和属性</h2> + +<h3 id="Widget_attributes">组件属性</h3> + +<div class="index"> + <ul> + <li>aria-autocomplete</li> + <li>aria-checked</li> + <li>aria-current</li> + <li>aria-disabled</li> + <li>aria-errormessage</li> + <li>aria-expanded</li> + <li>aria-haspopup</li> + <li>aria-hidden</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-invalid_attribute">aria-invalid</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-label_attribute">aria-label</a></li> + <li>aria-level</li> + <li>aria-modal</li> + <li>aria-multiline</li> + <li>aria-multiselectable</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-orientation_attribute">aria-orientation</a></li> + <li>aria-placeholder</li> + <li>aria-pressed</li> + <li>aria-readonly</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-required_attribute">aria-required</a></li> + <li>aria-selected</li> + <li>aria-sort</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-valuemax_attribute">aria-valuemax</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-valuemin_attribute">aria-valuemin</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-valuenow_attribute">aria-valuenow</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-valuetext_attribute">aria-valuetext</a></li> + </ul> +</div> + +<h3 id="Live_region_attributes">实时区域属性</h3> + +<div class="index"> + <ul> + <li>aria-live</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-relevant_attribute">aria-relevant</a></li> + <li>aria-atomic</li> + <li>aria-busy</li> + </ul> +</div> + +<h3 id="Drag_drop_attributes">拖放属性</h3> + +<div class="index"> + <ul> + <li>aria-dropeffect</li> + <li>aria-dragged</li> + </ul> +</div> + +<h3 id="Relationship_attributes">关系属性</h3> + +<div class="index"> + <ul> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-activedescendant_attribute">aria-activedescendant</a></li> + <li>aria-colcount</li> + <li>aria-colindex</li> + <li>aria-colspan</li> + <li>aria-controls</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-describedby_attribute">aria-describedby</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Annotations#associating_annotated_elements_with_their_details">aria-details</a></li> + <li>aria-errormessage</li> + <li>aria-flowto</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-labelledby_attribute">aria-labelledby</a></li> + <li>aria-owns</li> + <li>aria-posinset</li> + <li>aria-rowcount</li> + <li>aria-rowindex</li> + <li>aria-rowspan</li> + <li>aria-setsize</li> + </ul> +</div> + +<h3 id="MicrosoftEdge-specific_properties">MicrosoftEdge 特定的属性</h3> + +<div class="index"> + <ul> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques/x-ms-aria-flowfrom">x-ms-aria-flowfrom</a> {{Non-standard_Inline}}</li> + </ul> +</div> diff --git a/files/zh-cn/web/accessibility/aria/index.html b/files/zh-cn/web/accessibility/aria/index.html index da650a78f6..b59dc1e7ca 100644 --- a/files/zh-cn/web/accessibility/aria/index.html +++ b/files/zh-cn/web/accessibility/aria/index.html @@ -3,175 +3,123 @@ title: ARIA slug: Web/Accessibility/ARIA tags: - ARIA - - Accessibility - - NeedsTranslation - - TopicStub + - 可访问性 + - 无障碍 + - HTML translation_of: Web/Accessibility/ARIA --- -<p>Accessible Rich Internet Applications <strong>(ARIA)</strong> 是能够让残障人士更加便利的访问 Web 内容和使用 Web 应用(特别是那些由JavaScript 开发的)的一套机制。</p> -<p>ARIA是对超文本标记语言(HTML )的补充,以便在没有其他机制的情况下,使得应用程序中常用的交互和小部件可以传递给辅助交互技术。例如,ARIA支持HTML4中的可访问导航地标、JavaScript小部件、表单提示和错误消息、实时内容更新等。</p> +<p><strong>ARIA</strong> (Accessible Rich Internet Applications) 是一组属性,用于定义使残障人士更容易访问 Web 内容和 Web 应用程序(尤其是使用 JavaScript 开发的应用程序)的方法。</p> -<p>ARIA 是一组特殊的易用性属性,可以添加到任意标签上,尤其适用于 HTML。role 属性定义了对象的通用类型(例如文章、警告,或幻灯片)。额外的 ARIA 属性提供了其他有用的特性,例如表单的描述或进度条的当前值。</p> +<p>它补充了 HTML,以便在没有其他机制时可以将应用程序中常用的交互和小部件传递给辅助技术。例如,ARIA 支持 HTML4 中的可访问导航地标、JavaScript 小部件、表单提示和错误消息、实时内容更新等。</p> -<p>ARIA 在大多数流行的浏览器和屏幕阅读器中得到了实现。尽管如此,实现方式有所不同,而且旧的技术对其支持不好(或者不支持)。使用可以优雅降级的“安全的” ARIA,或者要求用户升级使用新的技术。</p> - -<div class="blockIndicator warning"> -<p>这些小部件中的许多后来被合并到HTML5中,如果存在这样的元素,<strong>开发人员应该更倾向使用对应的语义化HTML元素,而不是使用ARIA</strong>。例如,原生元素具有内置的<a href="https://developer.mozilla.org/zh-CN/docs/Web/Accessibility/Keyboard-navigable_JavaScript_widgets">键盘可访问性</a>、角色和状态。但是,如果您选择使用ARIA,您有责任在脚本中模仿(等效的)浏览器行为。</p> +<div class="notecard warning"> + <p>许多这些小部件后来被合并到 HTML5 中,如果存在这样的元素,<strong>开发人员应该更喜欢使用正确语义的 HTML 元素而不是使用 ARIA</strong>。例如,原生元素具有内置的<a href="/zh-CN/docs/Web/Accessibility/Keyboard-navigable_JavaScript_widgets">键盘可访问性</a>、角色和状态。但是,如果您选择使用 ARIA,则您有责任在脚本中模仿(等效)浏览器行为。</p> </div> +<p>这是进度条小部件的标记:</p> -<p>下面是一段进度条组件的代码:</p> - -<pre class="brush: html notranslate"><code><div id="percent-loaded" role="progressbar" aria-valuenow="75" - aria-valuemin="0" aria-valuemax="100" /></code> +<pre class="brush: html"><div id="percent-loaded" role="progressbar" aria-valuenow="75" + aria-valuemin="0" aria-valuemax="100"> +</div> </pre> -<p>由于这个滚动条是用<code><div></code>写的,没有字面含义。然而,对开发者来说,在HTML4中,又没有更多的语义化的标签,所以我们需要引入ARIA这个角色和属性,这些是通过向元素添加属性来指定的。举个例子,<code>role="progressbar"</code>这个属性告诉浏览器,该元素其实是一个JavaScript实现的进度条组件。<code>aria-valuemin </code>和<code>aria-valuemax</code> 属性表明了进度条的最小值和最大值。 <code>aria-valuenow</code>则描述了当前进度条的状态, 因此它得用JavaScript来更新。</p> +<p>这个进度条是使用 {{HTMLElement("div")}} 构建的,它没有任何意义。不幸的是,HTML 4 中没有更多语义标签可供开发人员使用,因此我们需要包含 ARIA 角色和属性。这些是通过向元素添加属性来指定的。在这个例子中, <code>role="progressbar"</code> 属性通知浏览器这个元素实际上是一个 JavaScript 驱动的进度条小部件。 <code>aria-valuemin</code> 和 <code>aria-valuemax</code> 属性指定进度条的最小值和最大值,<code>aria-valuenow</code> 描述进度条的当前状态和因此必须使用 JavaScript 保持更新。</p> -<p>除了直接给标签加属性,还可以通过JavaScript代码把ARIA属性添加到元素并动态地更新,如下面所示:</p> +<p>除了将它们直接放置在元素中之外,还可以将 ARIA 属性添加到元素中,并使用 JavaScript 代码动态更新,如下所示:</p> -<pre class="brush: js notranslate"><code>// Find the progress bar <div> in the DOM. - var progressBar = document.getElementById("percent-loaded");</code> +<pre class="brush: js">// Find the progress bar <div> in the DOM. +var progressBar = document.getElementById("percent-loaded"); -<code>// Set its ARIA roles and states, -// so that assistive technologies know what kind of widget it is.</code> - progressBar.setAttribute("role", "progressbar"); - progressBar.setAttribute("aria-valuemin", 0); - progressBar.setAttribute("aria-valuemax", 100); +// Set its ARIA roles and states, +// so that assistive technologies know what kind of widget it is. +progressBar.setAttribute("role", "progressbar"); +progressBar.setAttribute("aria-valuemin", 0); +progressBar.setAttribute("aria-valuemax", 100); // Create a function that can be called at any time to update // the value of the progress bar. - function updateProgress(percentComplete) { - progressBar.setAttribute("aria-valuenow", percentComplete); - } - -</pre> +function updateProgress(percentComplete) { + progressBar.setAttribute("aria-valuenow", percentComplete); +}</pre> -<div class="note"> -<p>注意由于ARIA是在HTML4之后引入的,所以在HTML4或XTHML中没有提供验证。然而,它提供的可访问性收益远远超过任何技术上的无效性。</p> +<div class="notecard note"> + <p>请注意,ARIA 是在 HTML4 之后发明的,因此无法在 HTML4 或其 XHTML 变体中进行验证。然而,它提供的可访问性收益远远超过任何技术无效性。</p> -<p>在HTML5中,所有的ARIA属性都是有效的。新的标记元素(<code><main></code>, <code><header></code>, <code><nav></code>等)都已具有了ARIA角色,所以就没必要再标注说明了。</p> + <p>在 HTML5 中,所有 ARIA 属性都经过验证。新的地标元素({{HTMLElement("main")}}、{{HTMLElement("header")}}、{{HTMLElement("nav")}} 等)具有内置的 ARIA 角色,因此有无需复制它们。</p> </div> -<table class="topicpage-table"> - <tbody> - <tr> - <td> - <h3 id="Documentation" name="Documentation">ARIA 入门</h3> - - <dl> - <dt><a href="/en-US/docs/Accessibility/An_overview_of_accessible_web_applications_and_widgets" title="An overview of accessible web applications and widgets">ARIA 介绍</a></dt> - <dd>关于借助 ARIA 使得动态内容可访问的快速介绍。也可参考经典的 <a class="external" href="http://dev.opera.com/articles/view/introduction-to-wai-aria/" title="http://dev.opera.com/articles/view/introduction-to-wai-aria/">ARIA 介绍 - Gez Lemon</a>,2008 年。</dd> - <dt><a href="/en-US/docs/Accessibility/ARIA/Web_applications_and_ARIA_FAQ" title="https://developer.mozilla.org/en-US/docs/Accessibility/Web_applications_and_ARIA_FAQ">Web 应用与 ARIA FAQ</a></dt> - <dd>解答关于WAI-ARIA的问题并且解释为什么网站需要ARIA。</dd> - <dt><a class="external" href="http://zomigi.com/blog/videos-of-screen-readers-using-aria-updated/" title="http://zomigi.com/blog/videos-of-screen-readers-using-aria-updated/">在视频频和屏幕阅读器使用 ARIA</a></dt> - <dd>从网上看到真实和简单的例子,包括“before”和“after”ARIA视频。</dd> - <dt><a class="external" href="http://dvcs.w3.org/hg/aria-unofficial/raw-file/tip/index.html">在 HTML 使用 ARIA</a></dt> - <dd>A practical guide for developers. It suggests what ARIA attributes to use on HTML elements. Suggestions are based on implementation realities.一份为开发人员提供实用指南。它建议一些基于基于执行的在HTML元素上使用哪些ARIA属性。</dd> - </dl> - - <h3 id="ARIA_初级进阶">ARIA 初级进阶</h3> - - <dl> - <dt><a class="external" href="http://www.paciellogroup.com/blog/2010/10/using-wai-aria-landmark-roles/" rel="external" title="http://www.paciellogroup.com/blog/2010/10/using-wai-aria-landmark-roles/">通过ARIA标签增强页面导航</a></dt> - <dd>A nice intro to using ARIA landmarks to improve web page navigation for screen reader users. <a class="external" href="http://www.paciellogroup.com/blog/2011/07/html5-accessibility-chops-aria-landmark-support/" rel="external" title="http://www.paciellogroup.com/blog/2011/07/html5-accessibility-chops-aria-landmark-support/">See also, ARIA landmark implementation notes</a> and examples on real sites (updated as of July '11).</dd> - <dt><a href="/en-US/docs/ARIA/forms" rel="internal" title="/en-US/docs/ARIA/forms">提高表单可访问性</a></dt> - <dd>ARIA is not just for dynamic content! Learn how to improve accessibility of HTML forms using additional ARIA attributes. </dd> - <dt><a class="external" href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Live_Regions" rel="external" title="http://www.freedomscientific.com/Training/Surfs-up/AriaLiveRegions.htm">ARIA 活动区域 </a></dt> - <dd>活动区域为屏幕阅读器提供关于如何处理一个页面内容变更的建议。</dd> - <dt><a class="external" href="http://www.freedomscientific.com/Training/Surfs-up/AriaLiveRegions.htm" rel="external" title="http://www.freedomscientific.com/Training/Surfs-up/AriaLiveRegions.htm">用ARIA 活动区域去通知内容的改变</a></dt> - <dd>A quick summary of live regions, by the makers of JAWS screen reader software. Note that live regions are also supported by NVDA in Firefox, and VoiceOver with Safari (as of OS X Lion and iOS 5).</dd> - </dl> - - <h3 id="ARIA_与脚本组件">ARIA 与脚本组件</h3> - - <dl> - <dt><a class="external" href="/en-US/docs/Accessibility/Keyboard-navigable_JavaScript_widgets" title="en-US/docs/Accessibility/Keyboard-navigable_JavaScript_widgets">键盘导航与组件焦点</a></dt> - <dd>The first step in developing an accessible JavaScript widget is to make it keyboard navigable. This article steps through the process. The <a class="external" href="http://www.yuiblog.com/blog/2009/02/23/managing-focus/" title="http://www.yuiblog.com/blog/2009/02/23/managing-focus/">Yahoo! focus management article</a> is a great resource as well.</dd> - <dt><a class="external" href="http://access.aol.com/dhtml-style-guide-working-group/" title="http://dev.aol.com/dhtml_style_guide">Style Guide for Keyboard Navigation</a></dt> - <dd>A challenge with ARIA is getting developers to implement consistent behavior -- clearly best for users. This style guide describes the keyboard interface for common widgets.</dd> - </dl> - - <h3 id="ARIA_资源">ARIA 资源</h3> - - <dl> - <dt><a href="/en-US/docs/Accessibility/ARIA/widgets/overview" title="en-US/docs/aria/widgets/overview">Widget Techniques, Tutorials, and Examples</a></dt> - <dd>Need a slider, a menu, or another kind of widget? Find resources here.</dd> - <dt><a class="external" href="http://www.paciellogroup.com/blog/2009/07/wai-aria-implementation-in-javascript-ui-libraries/" title="http://www.paciellogroup.com/blog/2009/07/wai-aria-implementation-in-javascript-ui-libraries/">ARIA-Enabled JavaScript UI Libraries</a></dt> - <dd>If you're starting a new project, choose a UI widget library with ARIA support already built-in. Warning: this is from 2009 -- content should be moved to an MDN page where it can be updated.</dd> - <dt><a class="external" href="http://dl.dropbox.com/u/573324/CSUN2012/index.html" title="http://dl.dropbox.com/u/573324/CSUN2012/index.html">Accessibility of HTML5 and Rich Internet Applications - CSUN 2012 Workshop Materials</a></dt> - <dd>Includes slide presentations and examples.</dd> - </dl> - </td> - <td> - <h3 id="Community" name="Community">邮件列表</h3> - - <dl> - <dt><a class="link-https" href="https://groups.google.com/forum/#!forum/free-aria" title="https://groups.google.com/forum/#!forum/free-aria">Free ARIA Google Group</a></dt> - <dd>A place to ask questions about ARIA, as well as make suggestions for improving the ARIA documentation found on these pages.</dd> - </dl> - - <h3 id="Community" name="Community">博客</h3> - - <p>ARIA information on blogs tends to get out of date quickly. Still, there is some great info out there from other developers making ARIA work today.</p> - - <p><a class="external" href="http://www.paciellogroup.com/blog/category/wai-aria/" title="http://www.paciellogroup.com/blog/category/wai-aria/">Paciello Group</a></p> - - <p><a class="external" href="http://www.accessibleculture.org/tag/wai-aria/" title="http://www.accessibleculture.org/tag/wai-aria/">Accessible Culture</a></p> - - <p><a class="external" href="http://yaccessibilityblog.com/library/category/code/aria" title="http://yaccessibilityblog.com/library/category/code/aria">Yahoo! Accessibility</a></p> - - <h3 id="提交_Bug">提交 Bug</h3> - - <p><a href="/en/Accessibility/ARIA/How_to_file_ARIA-related_bugs" title="https://developer.mozilla.org/en/ARIA/How_to_file_ARIA-related_bugs">File ARIA bugs on browsers, screen readers, and JavaScript libraries</a>.</p> - - <h3 id="示例">示例</h3> - - <dl> - <dt><a class="external" href="/en-US/docs/Accessibility/ARIA/ARIA_Test_Cases" title="en-US/docs/ARIA/examples">ARIA 示例库</a></dt> - <dd>A set of barebones example files which are easy to learn from.</dd> - <dt><span class="external">可访问的 JS 组件库演示</span></dt> - <dd><a class="external" href="http://dojotoolkit.org/widgets" title="http://dojotoolkit.org/widgets">Dojo</a>, <a class="external" href="http://hanshillen.github.com/jqtest/" title="http://hanshillen.github.com/jqtest/">jQuery</a>, <a class="external" href="http://wiki.fluidproject.org/display/fluid/Components" title="http://wiki.fluidproject.org/display/fluid/Components">Fluid</a>, <a class="external" href="http://yuilibrary.com/gallery/" title="http://yuilibrary.com/gallery/">YUI</a></dd> - </dl> - - <dl> - <dt><a class="external" href="http://mail.yahoo.com" title="http://mail.yahoo.com">Yahoo! 邮箱</a></dt> - <dd>Yahoo! puts it all together with Yahoo! Mail, a web app that almost looks like a native app. It works very well. As a <a class="external" href="http://www.marcozehe.de/2011/09/21/review-the-all-new-yahoo-mail-web-application/" title="http://www.marcozehe.de/2011/09/21/review-the-all-new-yahoo-mail-web-application/">review of Yahoo! Mail</a> by screen reader Marco Zehe says, "Keep up the good work!".</dd> - </dl> - - <dl> - <dt><a class="external" href="http://search.yahoo.com" title="http://search.yahoo.com">Yahoo! 搜索</a></dt> - <dd>Yahoo! has done an amazing job of advancing ARIA here, by exercising ARIA's full capabilities and <a class="external" href="http://ycorpblog.com/2011/03/23/searchdirect/" title="http://ycorpblog.com/2011/03/23/searchdirect/">sharing their techniques</a>. Yahoo! Search uses a combination of ARIA landmarks, live regions, and widgets.</dd> - </dl> - - <h3 id="规范特性">规范特性</h3> - - <dl> - <dt><a class="external" href="http://www.w3.org/WAI/intro/aria.php" title="http://www.w3.org/WAI/intro/aria.php">WAI-ARIA Activities Overview at W3C</a></dt> - <dd>Authoritative Overview of WAI-ARIA Standardization efforts by the Web Accessibility Initiative (WAI)</dd> - <dt><a class="external" href="http://www.w3.org/TR/wai-aria/" title="http://www.w3.org/TR/wai-aria/">WAI-ARIA Specification</a></dt> - <dd>The W3C specification itself, useful as a reference. Note that, at this stage, it is important to test compatibility, as implementations are still inconsistent.</dd> - <dt><a class="external" href="http://www.w3.org/WAI/PF/aria-practices/" title="http://www.w3.org/WAI/PF/aria-practices/">WAI-ARIA Authoring Practices</a></dt> - <dd>Like the W3C WAI-ARIA specification, the official best practices represents a future ideal -- a day when authors can rely on consistent ARIA support across browsers and screen readers. The W3C documents provide an in-depth view of ARIA.<br> - <br> - For now, web developers implementing ARIA should maximize compatibility. Use best practices docs and examples based on current implementations.</dd> - <dt><a class="external" href="http://www.openajax.org/member/wiki/Accessibility" title="http://www.openajax.org/member/wiki/Accessibility">Open AJAX Accessibility Task Force</a></dt> - <dd>The Open AJAX effort centers around developing tools, sample files, and automated tests for ARIA.</dd> - <dt><a href="/en-US/docs/Accessibility/ARIA/ARIA_Techniques" title="ARIA Techniques">Under Construction: WCAG 2.0 ARIA Techniques</a></dt> - <dd>The community needs a complete set of WCAG techniques for WAI-ARIA + HTML, so that organizations can be comfortable claiming their ARIA-enabled content is WCAG compliant. This is mostly important when regulations or policies are based on WCAG.</dd> - </dl> - </td> - </tr> - </tbody> - <tbody> - <tr> - <td colspan="2"> - <h3 id="Related_Topics" name="Related_Topics">相关主题</h3> - - <dl> - <dd><a href="/en-US/docs/Accessibility" title="en-US/docs/Accessibility">Accessibility</a>, <a href="/en-US/docs/AJAX" title="en-US/docs/AJAX">AJAX</a>, <a href="/en-US/docs/JavaScript" title="en-US/docs/JavaScript">JavaScript</a></dd> - </dl> - </td> - </tr> - </tbody> -</table> +<h2 id="Support">支持</h2> + +<p>与任何其他 Web 技术一样,对 ARIA 有不同程度的支持。支持基于所使用的操作系统和浏览器,以及与之交互的辅助技术类型。此外,操作系统、浏览器和辅助技术的版本也是影响因素。较旧的软件版本可能不支持某些 ARIA 角色、仅部分支持或误报其功能。</p> + +<p>同样重要的是要承认一些依赖辅助技术的人不愿意升级他们的软件,因为害怕失去与他们的计算机和浏览器交互的能力。因此,尽可能对 <a href="/zh-CN/docs/Learn/Accessibility/HTML">使用语义化的 HTML 元素</a> 很重要,因为语义 HTML 对辅助技术的支持要好得多。</p> + +<p>使用实际辅助技术测试您编写的 ARIA 也很重要。就像浏览器模拟器和模拟器不是测试完全支持的有效解决方案一样,代理辅助技术解决方案不足以完全保证功能。</p> + +<h2 id="Tutorials">教程</h2> + +<dl> + <dt><a href="/zh-CN/docs/Web/Accessibility/An_overview_of_accessible_web_applications_and_widgets">ARIA 简介</a></dt> + <dd>快速介绍如何使用 ARIA 访问动态内容。另请参阅 2008 年的经典 <a href="https://dev.opera.com/articles/view/introduction-to-wai-aria/">ARIA intro by Gez Lemon</a>。</dd> + <dt><a href="https://zomigi.com/blog/videos-of-screen-readers-using-aria-updated/">Videos of screen readers using ARIA</a></dt> + <dd>查看来自网络的真实和简化示例,视频包括 ARIA “之前”和 ARIA“之后”。</dd> + <dt><a href="https://w3c.github.io/using-aria/">Using ARIA</a></dt> + <dd>开发人员实用指南。它建议在 HTML 元素上使用哪些 ARIA 属性。建议是基于实施的实际情况。</dd> +</dl> + +<h2 id="Simple_ARIA_enhancements">简单的 ARIA 增强</h2> + +<dl> + <dt><a href="https://www.paciellogroup.com/blog/2013/02/using-wai-aria-landmarks-2013/">Enhancing page navigation with ARIA landmarks</a></dt> + <dd>使用 ARIA 地标改进屏幕阅读器用户的网页导航的一个很好的介绍。参考 <a href="https://www.paciellogroup.com/blog/2011/07/html5-accessibility-chops-aria-landmark-support/">ARIA landmark implementation notes</a> 和真实站点上的示例(截至 2011 年 7 月更新)。</dd> + <dt><a href="/zh-CN/docs/Web/Accessibility/ARIA/forms">Improving form accessibility</a></dt> + <dd>ARIA 不仅适用于动态内容!了解如何使用附加 ARIA 属性提高 HTML 表单的可访问性。</dd> +</dl> + +<h2 id="ARIA_for_Scripted_Widgets">脚本小部件的 ARIA</h2> + +<dl> + <dt><a href="/zh-CN/docs/Web/Accessibility/Keyboard-navigable_JavaScript_widgets">编写可通过键盘导航的 JavaScript 小部件</a></dt> + <dd>{{HTMLElement("input")}}、{{HTMLElement("button")}} 等内置元素具有内置键盘辅助功能。如果您使用 {{HTMLElement("div")}} 和 ARIA 来“伪造”这些,则必须确保您的小部件可通过键盘访问。</dd> + <dt><a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Live_Regions">实时区域</a></dt> + <dd>实时区域向屏幕阅读器提供有关如何处理页面内容更改的建议。</dd> + <dt><a href="https://www.freedomscientific.com/Training/Surfs-up/AriaLiveRegions.htm">Using ARIA Live Regions to announce content changes</a></dt> + <dd>JAWS 屏幕阅读器软件制造商对实时区域的快速总结。带有 Firefox 的 NVDA 和带有 Safari 的 VoiceOver 也支持实时区域。</dd> +</dl> + +<h2 id="References">参考文献</h2> + +<dl> + <dt><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles">ARIA Roles</a></dt> + <dd>参考页面涵盖了 MDN 上讨论的所有 WAI-ARIA 角色。</dd> +</dl> + +<h2 id="Standardization_Efforts">标准化工作</h2> + +<dl> + <dt><a href="https://www.w3.org/TR/wai-aria-1.1/">WAI-ARIA Specification</a></dt> + <dd>W3C 规范本身。</dd> + <dt><a href="https://www.w3.org/TR/wai-aria-practices-1.1/">WAI-ARIA Authoring Practices</a></dt> + <dd><p>官方最佳实践记录了如何最好地对常见小部件和交互进行 ARIA 化。一个很好的资源。</p></dd> +</dl> + +<h2 id="Videos">视频</h2> + +<p>以下演讲是了解 ARIA 的好方法:</p> + +<p><a href="https://www.youtube.com/watch?v=qdB8SRhqvFc">ARIA, Accessibility APIs and coding like you give a damn! – Léonie Watson</a></p> + +<h2 id="Filing_Bugs">提交错误</h2> + +<p><a href="/zh-CN/docs/Web/Accessibility/ARIA/How_to_file_ARIA-related_bugs">在浏览器、屏幕阅读器和 JavaScript 库上提交 ARIA 错误。</a></p> + +<h2 id="Related_Topics">相关话题</h2> + +<p><a href="/zh-CN/docs/Web/Accessibility">Accessibility</a>, <a href="/zh-CN/docs/Web/Guide/AJAX">AJAX</a>, <a href="/zh-CN/docs/Web/JavaScript">JavaScript</a></p> + +<section id="Quick_links"> + <ol> + <li><a href="/zh-CN/docs/Web/Guide">网页开发</a></li> + <li><a href="/zh-CN/docs/Mozilla/Accessibility">Mozilla 的可访问性</a></li> + </ol> +</section> diff --git a/files/zh-cn/web/accessibility/aria/roles/index.html b/files/zh-cn/web/accessibility/aria/roles/index.html new file mode 100644 index 0000000000..e32d5a1038 --- /dev/null +++ b/files/zh-cn/web/accessibility/aria/roles/index.html @@ -0,0 +1,86 @@ +--- +title: WAI-ARIA Roles +slug: Web/Accessibility/ARIA/Roles +tags: +- ARIA +- ARIA Roles +- 可访问性 +- 无障碍 +- Reference +- Roles +translation_of: Web/Accessibility/ARIA/Roles +--- +<p>此页面列出了涵盖 MDN 上讨论的所有 WAI-ARIA 角色的参考页面。有关角色的完整列表,请参阅 <a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques">ARIA 技术</a></p> + +<p>{{SubpagesWithSummaries}}</p> + +<div class="hidden"> + <p>请通过在 Role 名称后添加您的姓名来声明您想要从事的 Role:(旧页面从 <a href="/zh-CN/docs/Web/Accessibility/ARIA/ARIA_Techniques">https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques</a> 链接)。已完成初稿的已从以下列表中删除。</p> + + <ul> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Alertdialog_Role">alertdialog</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Banner_role">banner</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Combobox_Role">Combobox</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Command_Role">Command</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Columnheader_Role">Columnheader </a>(Estelle)</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Complementary_role">complementary</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Composite_Role">composite</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Definition_Role">Definition</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Directory_Role">Directory</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Feed_Role">Feed</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Gridcell_role">Gridcell</a> (Eric E)</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Group_Role">Group</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Input_Role">input</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Landmark_Role">landmark</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Link_Role">Link</a> - old page</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/listbox_role">listbox</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Log_Role">Log</a> - old page</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Marquee_Role">Marquee</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Math_Role">math</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Menu_Role">menu</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Menubar_Role">menubar</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Menuiitem_Role">Menuitem</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Menuitemcheckbox_Role">Menuitemcheckbox</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Menuitemradio_Role">Menuitemradio</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/None_Role">none</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Note_Role">note</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Option_Role">Option</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Presentation_Role">presentation</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Progressbar_Role">Progressbar</a> - old page</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Radio_Role">Radio</a> - old page</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Radiogroup_Role">radiogroup</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Range_Role">range</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Region_role">region</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Roletype_Role">roletype</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Rowheader_Role">rowheader</a>(Estelle)</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Scrollbar_Role">Scrollbar</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Searchbox_Role">searchbox</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Section_Role">section</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Sectionhead_Role">sectionhead</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Select_Role">select</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Separator_Role">separator</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Slider_Role">Slider</a> - old page</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Spinbutton_Role">Spinbutton</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Status_Role">Status</a> - old page</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Structure_Role">structure</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Tab_Role">tab</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Tablist_Role">tablist</a> (Michiel)</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Tabpanel_Role">Tabpanel</a> (Michiel)</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Term_Role">term</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Time_Role">Timer</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Toolbar_Role">toolbar</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Tooltip_Role">Tooltip</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Tree_Role">Tree</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Treegrid_Role">treegrid</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Treeitem_Role">Treeitem</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Widget_Role">widget</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/Window_Role">window</a></li> + </ul> +</div> + + +<section id="Quick_links"> + <ol> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles"><strong>WAI-ARIA roles</strong></a>{{ListSubpagesForSidebar("/en-US/docs/Web/Accessibility/ARIA/Roles")}}</li> + </ol> +</section> diff --git a/files/zh-cn/web/accessibility/aria/roles/list_role/index.html b/files/zh-cn/web/accessibility/aria/roles/list_role/index.html new file mode 100644 index 0000000000..788446c200 --- /dev/null +++ b/files/zh-cn/web/accessibility/aria/roles/list_role/index.html @@ -0,0 +1,122 @@ +--- +title: 'ARIA: list role' +slug: Web/Accessibility/ARIA/Roles/List_role +tags: +- ARIA +- ARIA Role +- ARIA list +- 可访问性 +- 无障碍 +- Reference +- Role +- list +--- +<p>ARIA <code>list</code> 角色可用于标识项目列表。它通常与 <code>listitem</code> 角色结合使用,该角色用于标识包含在列表中的列表项。</p> + +<pre class="brush: html"><section role="list"> + <div role="listitem">List item 1</div> + <div role="listitem">List item 2</div> + <div role="listitem">List item 3</div> +</section> +</pre> + +<h2 id="Description">描述</h2> + +<p>任何由外部容器和内部元素列表组成的内容都可以分别使用 <code>list</code> 和 <code>listitem</code> 容器识别为辅助技术。一个 <code>list</code> 必须有一个或多个 <code>listitem</code> 子级,或者,有一个或多个 <code>group</code> 作为子级,每个 <code>group</code>有一个或多个 <code>listitem</code> 作为子项。</p> + +<p>关于应该使用哪些元素来标记 <code>list</code> 和 <code>listitem</code> 没有硬性规定,但是您应该确保 <code>listitem</code> 在列表的上下文中有意义,例如购物清单、食谱步骤、行车路线。</p> + +<div class="notecard warning"> + <h4>警告</h4> + <p>如果可能,您应该使用适当的语义化 HTML 元素来标记 <code>list</code> 及其 <code>listitem</code>,如 {{HTMLElement("ul")}}、{{HTMLElement("ol")}} 和 {{HTMLElement("li")}}。有关完整示例,请参阅 {{anch("Best_practices", "最佳实践")}}。</p> +</div> + +<h3 id="Associated_WAI-ARIA_Roles_States_and_Properties">关联的 WAI-ARIA 角色、状态和属性</h3> + +<dl> + <dt><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Listitem_role">listitem</a></dt> + <dd><p>列表或目录中的单个项目。角色为 listitem 的元素只能在角色为 <code>list</code> 或 <code>group</code> 的元素中找到。</p></dd> + <dt><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/group_role">group</a></dt> + <dd><p>相关对象的集合,在嵌套在列表中时仅限于列表项,其重要性不足以在页面目录中拥有自己的位置。</p></dd> +</dl> + +<h3 id="Keyboard_Interactions">键盘交互</h3> + +<p>无</p> + +<h3 id="Required_JavaScript_features">所需的 JavaScript 功能</h3> + +<p>无</p> + +<h2 id="Examples">示例</h2> + +<p><a href="https://www.scottohara.me/blog/2018/05/26/aria-lists.html">ARIA Lists</a> — Scott O'Hara 的一些有用的例子和想法</p> + +<h2 id="Best_practices">最佳实践</h2> + +<p>仅在必要时使用 <code>role="list"</code> 和 <code>role="listitem"</code>。例如,无法控制 HTML 但能够在之后动态使用 JavaScript 提升可访问性的时候。</p> + +<p>与 HTML {{HTMLElement("ol")}} 和 {{HTMLElement("ul")}} 不同,ARIA <code>list</code> 角色不区分有序列表和无序列表。如果可能,您应该使用适当的语义 HTML 元素来标记列表({{HTMLElement("ol")}} 和 {{HTMLElement("ul")}})和列表项({{HTMLElement("li")}})。例如,我们上面的例子应该改写如下:</p> + +<pre class="brush: html"><ul> + <li>List item 1</li> + <li>List item 2</li> + <li>List item 3</li> +</ul></pre> + +<p>或者如果列表项的顺序很重要,则使用有序列表:</p> + +<pre class="brush: html"><ol> + <li>List item 1</li> + <li>List item 2</li> + <li>List item 3</li> +</ol></pre> + +<div class="notecard note"> + <h4>注意</h4> + <p>ARIA <code>list</code> / <code>listitem</code> 角色不区分有序列表和无序列表。</p> +</div> + +<p>顺便说一句,请注意,如果您使用 {{HTMLElement("ol")}} 或 {{HTMLElement("ul")}} 的语义 HTML 元素并应用 <a href="/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_presentation_role"><code>presentation</code></a> 角色,则每个子 <code>li</code> 元素都会继承 <code>presentation</code> 角色,因为 ARIA 需要 <code>listitem</code> 元素具有父 <code>list</code> 元素。因此,<code>li</code> 元素不会暴露给辅助技术,但是包含在这些 <code>li</code> 元素中的元素,包括嵌套列表,对辅助技术是可见的。</p> + +<div class="notecard note"> + <h4>注意</h4> + <p>如果要标记将用作选项卡式界面的项目列表,则应改为使用 <code>tab</code>、<code>tabpanel</code> 和 <code>tablist</code> 角色。</p> +</div> + +<h2 id="Specifications">规范</h2> + +<table> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + </tr> + <tr> + <td>{{SpecName("ARIA","#list","list")}}</td> + <td>{{Spec2('ARIA')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Screen_reader_support">屏幕阅读器支持</h2> + +<p>TBD</p> + +<h2 id="See_also">相关链接</h2> + +<ul> + <li><a href="https://wicg.github.io/aom/spec/">Accessibility Object Model</a></li> + <li><a href="https://w3c.github.io/html-aria/">ARIA in HTML</a></li> + <li>{{HTMLElement("ul")}}</li> + <li>{{HTMLElement("ol")}}</li> + <li>{{HTMLElement("li")}}</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Listitem_role">ARIA: listitem role</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/group_role">ARIA: group role</a></li> +</ul> + +<section id="Quick_links"> + <ol> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles"><strong>WAI-ARIA roles</strong></a>{{ListSubpagesForSidebar("/en-US/docs/Web/Accessibility/ARIA/Roles")}}</li> + </ol> +</section> diff --git a/files/zh-cn/web/accessibility/aria/roles/listbox_role/index.html b/files/zh-cn/web/accessibility/aria/roles/listbox_role/index.html new file mode 100644 index 0000000000..7c48e3add6 --- /dev/null +++ b/files/zh-cn/web/accessibility/aria/roles/listbox_role/index.html @@ -0,0 +1,220 @@ +--- +title: 'ARIA: listbox role' +slug: Web/Accessibility/ARIA/Roles/listbox_role +tags: + - ARIA + - 可访问性 + - 无障碍 +translation_of: Web/Accessibility/ARIA/Roles/listbox_role +--- +<p><code>listbox</code> 角色用于列表,用户可以从中选择一个或多个静态选项,并且与 HTML {{HTMLElement("select")}} 元素不同,它可能包含图像。</p> + +<h2 id="Description">描述</h2> + +<p><code>listbox</code> 角色用于标识创建列表的元素,用户可以从中选择一个或多个静态选项,类似于 HTML {{HTMLElement("select")}} 元素。与 {{HTMLElement("select")}} 不同,<code>listbox</code> 可以包含图像。<code>listbox</code> 的每个子项都应该有一个 <a href="https://www.w3.org/TR/2010/WD-wai-aria-20100916/roles#option"><code>option</code></a> role。</p> + +<p>强烈建议使用 HTML {{HTMLElement("select")}} 元素,如果只能选择一个选项,则使用一组单选按钮,如果可以选择多个选项,则使用一组复选框,因为有很多键盘交互来管理所有后代的焦点和原生 HTML 元素为您提供相关的功能。</p> + +<p>具有 <code>listbox</code> 角色的元素含有隐式的 <code>aria-orientation</code> 和 <code>vertical</code> 值。</p> + +<p>当一个列表被 tab 聚焦到时,如果没有其他内容,将会选择列表中的第一项。可以用 Up/Down 方向键在列表中导航,按 Shift + Up/Down 方向键将移动并扩展选择。键入一个或多个字母将在列表项中导航(相同的字母指向以那个开头的每个选项,不同的字母指向以整个字符串开头的第一个选项)。如果当前选项有关联的菜单, Shift+F10 将启动该菜单。如果项目可被勾选,Space 可用于切换 <a href="https://www.w3.org/TR/wai-aria-practices/#checkbox"><code>checkbox</code></a> role。对于可选择的列表项,Space 切换它们的选择,Shift+Space 可用于选择连续的项目,Ctrl+方向键 移动而不选择, Ctrl+Space 可用于选择非连续的项目。建议使用 checkbox、link 或其他方法来选择所有项目,为此可以使用 Ctrl+A 作为快捷键。</p> + +<p>当 listbox 角色被添加到元素中,或含有它的元素变得可见时,屏幕阅读器会在 listbox 获得焦点时读出它的 label 和 role。如果列表中的 option 或选项获得焦点,则接下来会读出它,如果屏幕阅读器支持,则会在列表中指示选项的位置。当焦点在列表中移动时,屏幕阅读器会读出相关选项。</p> + +<h3 id="Associated_ARIA_roles_states_and_properties">相关的 ARIA 角色、状态和属性</h3> + +<h4 id="Associated_Roles">相关角色</h4> + +<dl> + <dt><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/option_role">option</a></dt> + <dd>需要一个或多个嵌套的 <code>option</code>。 所有被选择的选项都含有 <code>aria-selected</code> 且值为 <code>true</code>。所有未选中的选项都含有 <code>aria-selected</code> 且值为 <code>false</code>。 如果某个选项不可选择,<code>aria-selected</code> 会被忽略。</dd> + <dt><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/List_role">list</a></dt> + <dd>包含 <code>listitem</code> 元素的部分</dd> +</dl> + +<h4 id="States_and_Properties">状态和属性</h4> + +<dl> + <dt>aria-activedescendant</dt> + <dd>保存 listbox 中当前活动元素的 <code>id</code> 字符串。如果这是一个 option 元素,那么这将是最近与之交互选项的 <code>id</code>,无论该选项是否具有值为 <code>true</code> 的 <code>aria-selected</code>。即使在多选列表框中,也只会有一个 <code>id</code>。如果 <code>id</code> 不引用 listbox 的 DOM 后代,则 <code>id</code> 必须包含在 <code>aria-owns</code> 属性中的 ID 中。</dd> + <dt>aria-owns</dt> + <dd>这是一个以空格分隔的元素 ID 列表,它们不是 listbox 的 DOM 子元素。此处列出的 ID 也不能列在任何其他元素的 <code>aria-owns</code> 属性中。</dd> + <dt>aria-multiselectable</dt> + <dd>如果用户可以选择多个选项,则存在并设置为 <code>true</code>。如果设置为 <code>true</code>,<em>每个</em> 可选的选项都应包含 <code>aria-selected</code> 属性并设置为 <code>true</code> 或 <code>false</code>。 <em>不可选</em>的选项<em>不应该</em>具有<code>aria-selected</code>属性。</dd> + <dd>如果值为 <code>false</code> 或被省略,那么仅当前选中的选项(如果有任何选项被选中)才需要<code>aria-selected</code>属性,而且必须设置为<code>true</code>。</dd> + <dt>aria-required</dt> + <dd>一个布尔属性,指示必须选择具有非空字符串值的选项。</dd> + <dt>aria-readonly</dt> + <dd>用户无法更改选择或取消选择,但 listbox 是可操作的。</dd> + <dt>aria-label</dt> + <dd>一个可供人类阅读的字符串,用于标识 listbox。如果有可见标签,则应使用 <code>aria-labelledby</code> 来引用该标签。</dd> + <dt>aria-labelledby</dt> + <dd>标识以空格分隔的元素 ID 列表中的一个或多个可见元素,这些元素标识 listbox。如果没有可见标签,则应该使用 <code>aria-label</code> 来包含标签。 (注意:带有两个 L 的 "labelled" 是基于辅助功能 API 约定的正确拼写。)</dd> + <dt>aria-roledescription</dt> + <dd>一个可供人类阅读的字符串,可以更清楚地标识 listbox 的作用。屏幕阅读器通常会在阅读标签(如果存在)后向用户阅读此值,而不是说 “listbox”。</dd> +</dl> + +<p>(有关更多详细信息以及 ARIA 状态和属性的完整列表,请参阅 <a href="https://www.w3.org/TR/wai-aria-1.1/#listbox">ARIA <code>listbox</code> (role)</a> 文档。 )</p> + +<h3 id="Keyboard_interactions">键盘交互</h3> + +<ul> + <li>当单选 listbox 获得焦点时: + <ul> + <li>如果在 listbox 获得焦点之前没有选择任何选项,则第一个选项将获得焦点。(可选)可以自动选择第一个选项。</li> + <li>如果在列表框获得焦点之前选择了一个选项,则焦点将设置在所选选项上。</li> + </ul> + </li> + <li>当多选 listbox 获得焦点时: + <ul> + <li>如果在列表框获得焦点之前没有选择任何选项,则焦点将设置在第一个选项上,并且选择状态不会自动更改。</li> + <li>如果在列表框获得焦点之前选择了一个或多个选项,则焦点将设置在列表中选定的第一个选项上。</li> + </ul> + </li> + <li><kbd>Down Arrow</kbd>: 将焦点移至下一个选项。(可选)在单选 listbox 中,选中的值也可以随焦点移动。</li> + <li><kbd>Up Arrow</kbd>: 将焦点移至上一个选项。(可选)在单选 listbox 中,选中的值也可以随焦点移动。</li> + <li><kbd>Home</kbd> (可选): 将焦点移至第一个选项。(可选)在单选列表框中,选中的值也可以随焦点移动。对于超过五个选项的列表,强烈建议支持此键。</li> + <li><kbd>End</kbd> (可选): 将焦点移至最后一个选项。(可选)在单选列表框中,选中的值也可以随焦点移动。对于超过五个选项的列表,强烈建议支持此键。</li> + <li>建议所有 listbox 都预先输入,尤其是那些有七个以上选项的列表框: + <ul> + <li>键入字符时:焦点移至名称符合键入的字符开头的一项。</li> + <li>快速连续键入多个字符时:焦点移至名称符合键入的字符串开头的一项。</li> + </ul> + </li> + <li><strong>多项选择</strong>: 作者可以实现两种交互模型中的任何一种来支持多选:推荐模型,不需要用户在导航列表时按住修饰键,例如 <kbd>Shift</kbd> 或 <kbd>Control</kbd> 或者需要在导航时按住修饰键以避免丢失选择状态的替代模型。 + <ul> + <li>推荐的选择模型——不需要按住修饰键: + <ul> + <li><kbd>Space</kbd>: 更改焦点选项的选择状态。</li> + <li><kbd>Shift + Down Arrow</kbd> (可选): 将焦点移动并切换到下一个选项的选定状态。</li> + <li><kbd>Shift + Up Arrow</kbd> (可选): 将焦点移动并切换到上一个选项的选定状态。</li> + <li><kbd>Shift + Space</kbd> (可选): 选择从最近选择的项目到焦点项目的连续项目。</li> + <li><kbd>Control + Shift + Home</kbd> (可选): 选择焦点选项和直到第一个选项的所有选项。 (可选)将焦点移至第一个选项。</li> + <li><kbd>Control + Shift + End</kbd> (可选): 选择焦点选项和直到最后一个选项的所有选项。 (可选)将焦点移到最后一个选项。</li> + <li><kbd>Control + A</kbd> (可选): 选择列表中的所有选项。(可选)如果选择了所有选项,它也可以取消选择所有选项。</li> + </ul> + </li> + </ul> + </li> +</ul> + +<h3 id="Required_JavaScript_features">所需的 JavaScript 功能</h3> + +<h4 id="selecting_an_option_in_a_single_select_listbox">在单选 listbox 中选择一个选项</h4> + +<p>当用户选择一个选项时,必须发生以下情况:</p> + +<ol> + <li>取消选择先前选择的选项,将 aria-selected 设置为 false,或完全删除该属性,将新未选择的选项的外观更改为未选择的。</li> + <li>选择新选择的选项,在该选项上设置 aria-selected="true" 并将新选择的选项的外观更改为选中。</li> + <li>将列表框上的 <code>aria-activedescendant</code> 值更新为新选择的选项的 id</li> + <li>可视化处理选项的丢焦、聚焦和选定状态</li> +</ol> + +<h4 id="Toggling_the_state_of_an_option_in_a_multi_select_listbox">在多选列表框中切换选项的状态</h4> + +<p>当用户点击一个选项、聚焦在一个选项时按下 <kbd>Space</kbd>或者以其他方式切换一个选项的状态,必须发生以下情况:</p> + +<ol> + <li>切换当前聚焦选项的 aria-selected 状态,如果它是 false,则将 aria-selected 的状态更改为 true,如果为 true,则将其更改为 false。</li> + <li>更改选项的外观以反映其选定状态</li> + <li>将列表框上的 aria-activedescendant 值更新为用户刚刚与之交互的选项的 id,即使他们将选项切换为取消选择。</li> +</ol> + +<div class="notecard note"> + <p>ARIA 使用的第一原则是您可以使用具有内置语义和行为的原生功能,而不是重新利用元素并<strong>添加</strong> ARIA角色、状态或属性使其可访问。含有后代 {{HTMLElement("option")}} 元素的 {{HTMLElement("select")}} 元素带有所有需要的交互的原生处理方法。</p> +</div> + +<h2 id="Examples">例子</h2> + +<h4 id="Example_1_A_single_select_listbox_that_uses_aria-activedescendant">例子 1: 使用 aria-activedescendant 的单个选择列表框</h4> + +<p>下面的代码片段显示了如何将列表框角色直接添加到 html 源代码中。</p> + +<pre class="brush: html"><p id="listbox1label" role="label">Select a color:</p> +<div role="listbox" tabindex="0" id="listbox1" aria-labelledby="listbox1label" + onclick="return listItemClick(event);" + onkeydown="return listItemKeyEvent(event);" + onkeypress="return listItemKeyEvent(event);" + aria-activedescendant="listbox1-1"> + <div role="option" id="listbox1-1" class="selected" aria-selected="true">Green</div> + <div role="option" id="listbox1-2">Orange</div> + <div role="option" id="listbox1-3">Red</div> + <div role="option" id="listbox1-4">Blue</div> + <div role="option" id="listbox1-5">Violet</div> + <div role="option" id="listbox1-6">Periwinkle</div> +</div> +</pre> + +<p>使用原生的 HTML {{HTMLElement("select")}} 和 {{HTMLElement("label")}} 元素可以更简单。</p> + +<pre class="brush: html"><label for="listbox1">Select a color:</label> +<select id="listbox1"> + <option selected>Green</option> + <option>Orange</option> + <option>Red</option> + <option>Blue</option> + <option>Violet</option> + <option>Periwinkle</option> +</select></pre> + +<h4 id="More_examples">更多例子</h4> + +<ul> + <li><a href="https://w3c.github.io/aria-practices/examples/listbox/listbox-scrollable.html">可滚动的 listbox 示例</a>: 滚动显示更多选项的单选 listbox,类似于 HTML <code>select</code> 的 <code>size</code> 属性大于 1。</li> + <li><a href="https://w3c.github.io/aria-practices/examples/listbox/listbox-collapsible.html">可折叠下拉 listbox 示例</a>: 激活时展开的单选可折叠 listbox,类似于 HTML <code>select</code> 并且属性 <code>size="1"</code>。</li> + <li><a href="https://w3c.github.io/aria-practices/examples/listbox/listbox-rearrangeable.html">具有可重新排列选项的 listbox 示例</a>: 带有可以添加、移动和删除选项的附带工具栏的单选和多选 listbox 的示例。</li> +</ul> + +<h2 id="Best_practices">最佳实践</h2> + +<ul> + <li>为了便于键盘访问,作者应该对这个角色的所有后代进行 <a href="https://www.w3.org/TR/wai-aria-1.1/#managingfocus">焦点管理</a></li> + <li>建议作者在列表未聚焦时使用不同的样式进行选择,例如非活动选项通常以较浅的背景颜色显示。</li> + <li>如果 listbox 不是另一个组件的一部分,那么它应该具有 <code>aria-labelledby</code> 属性。</li> + <li>如果一个或多个条目不是 listbox 的 DOM 子项,则需要设置额外的 <code>aria-*</code> 属性(参见 <a href="https://www.w3.org/TR/wai-aria-practices/#listbox_div">ARIA Best Practices</a>)。</li> + <li>如果有理由 <a href="https://www.w3.org/TR/wai-aria-1.1/#aria-expanded">展开</a> 列表框,<a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/combobox_role"><code>combobox</code></a> 角色可能更合适。</li> +</ul> + +<h2 id="Specifications">规范</h2> + +<table> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + </tr> + <tr> + <td>{{SpecName("ARIA","#listbox","ARIA listbox role")}}</td> + <td>{{Spec2('ARIA')}}</td> + </tr> + <tr> + <td>{{SpecName("ARIA Authoring Practices","#Listbox","Listbox Role")}}</td> + <td>{{Spec2('ARIA Authoring Practices')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Screen_reader_support">屏幕阅读器支持</h2> + +<p>TBD</p> + +<h2 id="See_also">另见</h2> + +<ul> + <li>{{HTMLElement("select")}} 元素</li> + <li>{{HTMLElement("label")}} 元素</li> + <li>{{HTMLElement("option")}} 元素</li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/combobox_role">ARIA: <code>combobox</code> 角色</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/option_role">ARIA: <code>option</code> 角色</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/List_role">ARIA: <code>list</code> 角色</a></li> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles/Listitem_role">ARIA: <code>listitem</code> 角色</a></li> + <li><a href="https://www.w3.org/TR/wai-aria-practices/#Listbox">ARIA Best Practices – Listbox</a></li> + <li><a href="https://www.w3.org/TR/wai-aria-1.1/#listbox">ARIA Role Model – Listbox</a></li> +</ul> + +<section id="Quick_links"> + <ol> + <li><a href="/zh-CN/docs/Web/Accessibility/ARIA/Roles"><strong>WAI-ARIA roles</strong></a>{{ListSubpagesForSidebar("/en-US/docs/Web/Accessibility/ARIA/Roles")}}</li> + </ol> +</section> diff --git a/files/zh-cn/web/accessibility/aria/roles/listitem_role/index.html b/files/zh-cn/web/accessibility/aria/roles/listitem_role/index.html new file mode 100644 index 0000000000..a4f9bb691d --- /dev/null +++ b/files/zh-cn/web/accessibility/aria/roles/listitem_role/index.html @@ -0,0 +1,120 @@ +--- +title: 'ARIA: listitem role' +slug: Web/Accessibility/ARIA/Roles/Listitem_role +tags: +- ARIA +- ARIA Role +- ARIA listitem +- 可访问性 +- 无障碍 +- Reference +- Role +- listitem +--- +<p>ARIA <code>listitem</code> 角色可用于标识项目列表中的项目。它通常与 <code>list</code> 角色结合使用,用于标识列表容器。</p> + +<pre class="brush: html"><section role="list"> + <div role="listitem">List item 1</div> + <div role="listitem">List item 2</div> + <div role="listitem">List item 3</div> +</section> +</pre> + +<h2 id="Description">描述</h2> + +<p>任何由外部容器和内部元素列表组成的内容都可以分别使用 <code>list</code> 和 <code>listitem</code> 容器识别为辅助技术。</p> + +<p>关于应该使用哪些元素来标记 <code>list</code> 和 <code>listitem</code> 没有硬性规定,但是您应该确保 <code>listitem</code> 在列表的上下文中有意义,例如购物清单、食谱步骤、行车路线。</p> + +<div class="notecard warning"> + <h4>警告</h4> + <p>如果可能,您应该使用适当的语义化 HTML 元素来标记 <code>list</code> 及其 <code>listitem</code>,如 {{HTMLElement("ul")}}、{{HTMLElement("ol")}} 和 {{HTMLElement("li")}}。有关完整示例,请参阅 {{anch("Best_practices", "最佳实践")}}。</p> +</div> + +<h3 id="Associated_WAI-ARIA_Roles_States_and_Properties">关联的 WAI-ARIA 角色、状态和属性</h3> + +<dl> + <dt><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/List_role">list</a></dt> + <dd><p>项目列表。角色为 <code>list</code> 的元素必须有一个或多个角色为 <code>listitem</code> 的元素作为子元素,一个或多个角色为 <code>group</code> 的元素具有一个或多个具有 <code>listitem</code> 角色的元素作为子元素。</p></dd> + <dt><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/group_role">group</a></dt> + <dd><p>相关对象的集合,在嵌套在列表中时仅限于列表项,其重要性不足以在页面目录中拥有自己的位置。</p></dd> +</dl> + +<h3 id="Keyboard_Interactions">键盘交互</h3> + +<p>无</p> + +<h3 id="Required_JavaScript_features">所需的 JavaScript 功能</h3> + +<p>无</p> + +<h2 id="Examples">示例</h2> + +<p><a href="https://www.scottohara.me/blog/2018/05/26/aria-lists.html">ARIA Lists</a> — Scott O'Hara 的一些有用的例子和想法</p> + +<h2 id="Best_practices">最佳实践</h2> + +<p>仅在必要时使用 <code>role="list"</code> 和 <code>role="listitem"</code>。例如,无法控制 HTML 但能够在之后动态使用 JavaScript 提升可访问性的时候。</p> + +<p>与 HTML {{HTMLElement("ol")}} 和 {{HTMLElement("ul")}} 不同,ARIA <code>list</code> 角色不区分有序列表和无序列表。如果可能,您应该使用适当的语义 HTML 元素来标记列表({{HTMLElement("ol")}} 和 {{HTMLElement("ul")}})和列表项({{HTMLElement("li")}})。例如,我们上面的例子应该改写如下:</p> + +<pre class="brush: html"><ul> + <li>List item 1</li> + <li>List item 2</li> + <li>List item 3</li> +</ul></pre> + +<p>或者如果列表项的顺序很重要,则使用有序列表:</p> + +<pre class="brush: html"><ol> + <li>List item 1</li> + <li>List item 2</li> + <li>List item 3</li> +</ol></pre> + +<div class="notecard note"> + <h4>注意</h4> + <p>ARIA <code>list</code> / <code>listitem</code> 角色不区分有序列表和无序列表。</p> +</div> + +<div class="notecard note"> + <h4>注意</h4> + <p>如果要标记将用作选项卡式界面的项目列表,则应改为使用 <code>tab</code>、<code>tabpanel</code> 和 <code>tablist</code> 角色。</p> +</div> + +<h2 id="Specifications">规范</h2> + +<table> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + </tr> + <tr> + <td>{{SpecName("ARIA","#listitem","listitem")}}</td> + <td>{{Spec2('ARIA')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Screen_reader_support">屏幕阅读器支持</h2> + +<p>TBD</p> + +<h2 id="See_also">相关链接</h2> + +<ul> + <li><a href="https://wicg.github.io/aom/spec/">Accessibility Object Model</a></li> + <li><a href="https://w3c.github.io/html-aria/">ARIA in HTML</a></li> + <li>{{HTMLElement("ul")}}</li> + <li>{{HTMLElement("ol")}}</li> + <li>{{HTMLElement("li")}}</li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/List_role">ARIA: list role</a></li> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles/group_role" rel="nofollow">ARIA: group role</a></li> +</ul> + +<section id="Quick_links"> + <ol> + <li><a href="/en-US/docs/Web/Accessibility/ARIA/Roles"><strong>WAI-ARIA roles</strong></a>{{ListSubpagesForSidebar("/en-US/docs/Web/Accessibility/ARIA/Roles")}}</li> + </ol> +</section> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/atob/index.html b/files/zh-cn/web/api/atob/index.html index f664a3908e..4679dec8c5 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/atob/index.html +++ b/files/zh-cn/web/api/atob/index.html @@ -1,6 +1,6 @@ --- title: WindowOrWorkerGlobalScope.atob() -slug: Web/API/WindowOrWorkerGlobalScope/atob +slug: Web/API/atob tags: - API - Base64 @@ -10,7 +10,7 @@ tags: - 参考 - 方法 translation_of: Web/API/WindowOrWorkerGlobalScope/atob -original_slug: Web/API/WindowBase64/atob +original_slug: Web/API/WindowOrWorkerGlobalScope/atob --- <p>{{APIRef("HTML DOM")}}</p> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/btoa/index.html b/files/zh-cn/web/api/btoa/index.html index 83873fc559..1ff44702a1 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/btoa/index.html +++ b/files/zh-cn/web/api/btoa/index.html @@ -1,6 +1,6 @@ --- title: WindowOrWorkerGlobalScope.btoa() -slug: Web/API/WindowOrWorkerGlobalScope/btoa +slug: Web/API/btoa tags: - API - Base64 @@ -11,7 +11,7 @@ tags: - 数据 - 方法 translation_of: Web/API/WindowOrWorkerGlobalScope/btoa -original_slug: Web/API/WindowBase64/btoa +original_slug: Web/API/WindowOrWorkerGlobalScope/btoa --- <p>{{APIRef("HTML DOM")}}</p> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/caches/index.html b/files/zh-cn/web/api/caches/index.html index 8debcc3322..73c9fe5b30 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/caches/index.html +++ b/files/zh-cn/web/api/caches/index.html @@ -1,7 +1,8 @@ --- title: WindowOrWorkerGlobalScope.caches -slug: Web/API/WindowOrWorkerGlobalScope/caches +slug: Web/API/caches translation_of: Web/API/WindowOrWorkerGlobalScope/caches +original_slug: Web/API/WindowOrWorkerGlobalScope/caches --- <p>{{APIRef()}}{{SeeCompatTable}}</p> diff --git a/files/zh-cn/web/api/canvas_api/index.html b/files/zh-cn/web/api/canvas_api/index.html index c10f589b23..b2bb060ed4 100644 --- a/files/zh-cn/web/api/canvas_api/index.html +++ b/files/zh-cn/web/api/canvas_api/index.html @@ -135,4 +135,4 @@ ctx.fillRect(10, 10, 150, 100);</code></pre> <li><a href="/en-US/docs/Web/WebGL">WebGL</a></li> </ul> -<section id="Quick_Links"></section> +<section id="Quick_links"></section> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/clearinterval/index.html b/files/zh-cn/web/api/clearinterval/index.html index 442e1bb81e..3ba4544b4b 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/clearinterval/index.html +++ b/files/zh-cn/web/api/clearinterval/index.html @@ -1,13 +1,13 @@ --- title: WindowTimers.clearInterval() -slug: Web/API/WindowOrWorkerGlobalScope/clearInterval +slug: Web/API/clearInterval tags: - API - WindowOrWorkerGlobalScope - 参考 - 方法 translation_of: Web/API/WindowOrWorkerGlobalScope/clearInterval -original_slug: Web/API/Window/clearInterval +original_slug: Web/API/WindowOrWorkerGlobalScope/clearInterval --- <div>{{ApiRef("HTML DOM")}}</div> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/cleartimeout/index.html b/files/zh-cn/web/api/cleartimeout/index.html index 4e700ea7d0..30648c48b7 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/cleartimeout/index.html +++ b/files/zh-cn/web/api/cleartimeout/index.html @@ -1,12 +1,12 @@ --- title: WindowOrWorkerGlobalScope.clearTimeout() -slug: Web/API/WindowOrWorkerGlobalScope/clearTimeout +slug: Web/API/clearTimeout tags: - API - WindowOrWorkerGlobalScope - clearTimeout translation_of: Web/API/WindowOrWorkerGlobalScope/clearTimeout -original_slug: Web/API/WindowTimers/clearTimeout +original_slug: Web/API/WindowOrWorkerGlobalScope/clearTimeout --- <div> <div>{{APIRef("HTML DOM")}}</div> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/createimagebitmap/index.html b/files/zh-cn/web/api/createimagebitmap/index.html index 7d55152960..c71c592551 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/createimagebitmap/index.html +++ b/files/zh-cn/web/api/createimagebitmap/index.html @@ -1,7 +1,8 @@ --- title: self.createImageBitmap() -slug: Web/API/WindowOrWorkerGlobalScope/createImageBitmap +slug: Web/API/createImageBitmap translation_of: Web/API/WindowOrWorkerGlobalScope/createImageBitmap +original_slug: Web/API/WindowOrWorkerGlobalScope/createImageBitmap --- <div>{{APIRef("Canvas API")}}</div> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/crossoriginisolated/index.html b/files/zh-cn/web/api/crossoriginisolated/index.html index ad19e53c2e..ff327d5d07 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/crossoriginisolated/index.html +++ b/files/zh-cn/web/api/crossoriginisolated/index.html @@ -1,7 +1,8 @@ --- title: WindowOrWorkerGlobalScope.crossOriginIsolated -slug: Web/API/WindowOrWorkerGlobalScope/crossOriginIsolated +slug: Web/API/crossOriginIsolated translation_of: Web/API/WindowOrWorkerGlobalScope/crossOriginIsolated +original_slug: Web/API/WindowOrWorkerGlobalScope/crossOriginIsolated --- <div>{{APIRef()}}{{SeeCompatTable}}</div> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/fetch/index.html b/files/zh-cn/web/api/fetch/index.html index 3ebf7d8fdb..a1ee53fadf 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/fetch/index.html +++ b/files/zh-cn/web/api/fetch/index.html @@ -1,6 +1,6 @@ --- title: WorkerOrGlobalScope.fetch() -slug: Web/API/WindowOrWorkerGlobalScope/fetch +slug: Web/API/fetch tags: - API - Experimental @@ -9,6 +9,7 @@ tags: - GlobalFetch - request translation_of: Web/API/WindowOrWorkerGlobalScope/fetch +original_slug: Web/API/WindowOrWorkerGlobalScope/fetch --- <p>{{APIRef("Fetch")}}</p> diff --git a/files/zh-cn/web/api/headers/getall/index.html b/files/zh-cn/web/api/headers/getall/index.html deleted file mode 100644 index a9a2d22e59..0000000000 --- a/files/zh-cn/web/api/headers/getall/index.html +++ /dev/null @@ -1,134 +0,0 @@ ---- -title: Headers.getAll() -slug: Web/API/Headers/getAll -translation_of: Web/API/Headers/getAll ---- -<p>{{APIRef("Fetch")}}{{ SeeCompatTable() }}</p> - -<p><strong><code>getAll()</code></strong> 方法会以数组形式返回指定header的所有值. 如果指定的header未存在,则返回一个空数组.</p> - -<div class="note"> -<p><strong>Note:</strong>出于安全原因, 部分头信息只能被用户代理控制. 这些头信息包括 {{Glossary("Forbidden_header_name", "forbidden header names", 1)}} 和 {{Glossary("Forbidden_response_header_name", "forbidden response header names", 1)}}.</p> -</div> - -<h2 id="Syntax">Syntax</h2> - -<pre class="brush: js">myHeaders.getAll(name);</pre> - -<h3 id="Parameters">Parameters</h3> - -<dl> - <dt><em>name</em></dt> - <dd>需检索的HTTP header名称. 如果HTTP header中不存在指定名称<span style="line-height: 19.0909080505371px;">则抛出一个 </span><span style="line-height: 19.0909080505371px;">{{jsxref("TypeError")}}.</span></dd> -</dl> - -<h3 id="Returns">Returns</h3> - -<p>An {{domxref("Array")}} containing a {{domxref("ByteString")}} sequence representing the values of the retrieved header.</p> - -<h2 id="Example">Example</h2> - -<p>Creating an empty <code>Headers</code> object is simple:</p> - -<pre class="brush: js">var myHeaders = new Headers(); // Currently empty</pre> - -<p>You could add a header to this using {{domxref("Headers.append")}}, then retrieve it using <code>getAll()</code>:</p> - -<pre class="brush: js">myHeaders.append('Content-Type', 'image/jpeg'); -myHeaders.getAll('Content-Type'); // Returns [ "image/jpeg" ] -</pre> - -<p>If the header has multiple values associated with it, the array will contain all the values, in the order they were added to the Headers object:</p> - -<pre class="brush: js">myHeaders.append('Accept-Encoding', 'deflate'); -myHeaders.append('Accept-Encoding', 'gzip'); -myHeaders.getAll('Accept-Encoding'); // Returns [ "deflate", "gzip" ]</pre> - -<div class="note"> -<p><strong>Note</strong>: Use {{domxref("Headers.get")}} to return only the first value added to the <code>Headers</code> object.</p> -</div> - -<h2 id="Specifications">Specifications</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Specification</th> - <th scope="col">Status</th> - <th scope="col">Comment</th> - </tr> - <tr> - <td>{{SpecName('Fetch','#dom-headers-getall','getAll()')}}</td> - <td>{{Spec2('Fetch')}}</td> - <td> </td> - </tr> - </tbody> -</table> - -<h2 id="Browser_compatibility">Browser compatibility</h2> - -<p>{{ CompatibilityTable() }}</p> - -<div id="compat-desktop"> -<table class="compat-table"> - <tbody> - <tr> - <th>Feature</th> - <th>Chrome</th> - <th>Firefox (Gecko)</th> - <th>Internet Explorer</th> - <th>Opera</th> - <th>Safari (WebKit)</th> - </tr> - <tr> - <td>Basic support</td> - <td>{{ CompatChrome(42) }}<br> - {{ CompatChrome(41) }} behind pref<br> - </td> - <td>{{ CompatGeckoDesktop(39)}}<br> - 34 behind pref</td> - <td>{{ CompatNo }}</td> - <td> - <p>29<br> - 28 behind pref</p> - </td> - <td>{{ CompatNo }}</td> - </tr> - </tbody> -</table> -</div> - -<div id="compat-mobile"> -<table class="compat-table"> - <tbody> - <tr> - <th>Feature</th> - <th>Android</th> - <th>Firefox Mobile (Gecko)</th> - <th>Firefox OS (Gecko)</th> - <th>IE Phone</th> - <th>Opera Mobile</th> - <th>Safari Mobile</th> - <th>Chrome for Android</th> - </tr> - <tr> - <td>Basic support</td> - <td>{{ CompatNo }}</td> - <td>{{ CompatNo }}</td> - <td>{{ CompatNo }}</td> - <td>{{ CompatNo }}</td> - <td>{{ CompatNo }}</td> - <td>{{ CompatNo }}</td> - <td>{{ CompatNo }}</td> - </tr> - </tbody> -</table> -</div> - -<h2 id="See_also">See also</h2> - -<ul> - <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li> - <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li> - <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li> -</ul> diff --git a/files/zh-cn/web/api/index/index.html b/files/zh-cn/web/api/index/index.html deleted file mode 100644 index e910b907d2..0000000000 --- a/files/zh-cn/web/api/index/index.html +++ /dev/null @@ -1,7 +0,0 @@ ---- -title: 指数 -slug: Web/API/Index -translation_of: Web/API/Index -original_slug: Web/API/指数 ---- -<p>{{Index("/zh-CN/docs/Web/API")}}</p> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/indexeddb/index.html b/files/zh-cn/web/api/indexeddb/index.html index 1c1f17f52c..baf7f0c1b9 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/indexeddb/index.html +++ b/files/zh-cn/web/api/indexeddb/index.html @@ -1,6 +1,6 @@ --- title: WindowOrWorkerGlobalScope.indexedDB -slug: Web/API/WindowOrWorkerGlobalScope/indexedDB +slug: Web/API/indexedDB tags: - API - Database @@ -11,6 +11,7 @@ tags: - 参考 - 属性 translation_of: Web/API/WindowOrWorkerGlobalScope/indexedDB +original_slug: Web/API/WindowOrWorkerGlobalScope/indexedDB --- <p>{{ APIRef() }}</p> diff --git a/files/zh-cn/web/api/intersection_observer_api/index.html b/files/zh-cn/web/api/intersection_observer_api/index.html index 16ec1fcc7f..37e64b709a 100644 --- a/files/zh-cn/web/api/intersection_observer_api/index.html +++ b/files/zh-cn/web/api/intersection_observer_api/index.html @@ -2,14 +2,21 @@ title: Intersection Observer API slug: Web/API/Intersection_Observer_API tags: - - Intersection Observer API - - IntersectionObserver +- API +- Clipping +- Intersection +- Intersection Observer API +- IntersectionObserver +- Overview +- Performance +- Reference +- Web translation_of: Web/API/Intersection_Observer_API --- <div> <p>{{DefaultAPISidebar("Intersection Observer API")}}</p> -<p class="summary">Intersection Observer API提供了一种异步检测目标元素与祖先元素或 {{Glossary("viewport")}} 相交情况变化的方法。</p> +<p class="summary">Intersection Observer API 提供了一种异步检测目标元素与祖先元素或 {{Glossary("viewport")}} 相交情况变化的方法。</p> </div> <p>过去,要检测一个元素是否可见或者两个元素是否相交并不容易,很多解决办法不可靠或性能很差。然而,随着互联网的发展,这种需求却与日俱增,比如,下面这些情况都需要用到相交检测:</p> @@ -21,7 +28,7 @@ translation_of: Web/API/Intersection_Observer_API <li>在用户看见某个区域时执行任务或播放动画</li> </ul> -<p>过去,相交检测通常要用到事件监听,并且需要频繁调用{{domxref("Element.getBoundingClientRect()")}} 方法以获取相关元素的边界信息。事件监听和调用 {{domxref("Element.getBoundingClientRect()")}} 都是在主线程上运行,因此频繁触发、调用可能会造成性能问题。这种检测方法极其怪异且不优雅。</p> +<p>过去,相交检测通常要用到事件监听,并且需要频繁调用 {{domxref("Element.getBoundingClientRect()")}} 方法以获取相关元素的边界信息。事件监听和调用 {{domxref("Element.getBoundingClientRect()")}} 都是在主线程上运行,因此频繁触发、调用可能会造成性能问题。这种检测方法极其怪异且不优雅。</p> <p>假如有一个无限滚动的网页,开发者使用了一个第三方库来管理整个页面的广告,又用了另外一个库来实现消息盒子和点赞,并且页面有很多动画(译注:动画往往意味着较高的性能消耗)。两个库都有自己的相交检测程序,都运行在主线程里,而网站的开发者对这些库的内部实现知之甚少,所以并未意识到有什么问题。但当用户滚动页面时,这些相交检测程序就会在页面滚动回调函数里不停触发调用,造成性能问题,体验效果让人失望。</p> @@ -31,11 +38,11 @@ translation_of: Web/API/Intersection_Observer_API <h2 id="Intersection_observer_的概念和用法">Intersection observer 的概念和用法</h2> -<p>Intersection Observer API 允许你配置一个回调函数,当以下情况发生时会被调用</p> +<p>Intersection Observer API 允许你配置一个回调函数,当以下情况发生时会被调用</p> <ul> <li>每当目标(<strong>target</strong>)元素与设备视窗或者其他指定元素发生交集的时候执行。设备视窗或者其他元素我们称它为根元素或根(<strong>root</strong>)。</li> - <li>Observer第一次监听目标元素的时候</li> + <li>Observer 第一次监听目标元素的时候</li> </ul> <p>通常,您需要关注文档最接近的可滚动祖先元素的交集更改,如果元素不是可滚动元素的后代,则默认为设备视窗。如果要观察相对于根(<strong>root</strong>)元素的交集,请指定根(<strong>root</strong>)元素为<code>null</code>。</p> @@ -46,97 +53,528 @@ translation_of: Web/API/Intersection_Observer_API <h3 id="创建一个_intersection_observer">创建一个 intersection observer</h3> -<p>创建一个 IntersectionObserver对象,并传入相应参数和回调用函数,该回调函数将会在目标(<strong>target</strong>)元素和根(<strong>root</strong>)元素的交集大小超过阈值(<strong>threshold</strong>)规定的大小时候被执行。</p> +<p>创建一个 IntersectionObserver 对象,并传入相应参数和回调用函数,该回调函数将会在目标(<strong>target</strong>)元素和根(<strong>root</strong>)元素的交集大小超过阈值(<strong>threshold</strong>)规定的大小时候被执行。</p> -<pre class="brush: js notranslate">let options = { - root: document.querySelector('#scrollArea'), - rootMargin: '0px', - threshold: 1.0 +<pre class="brush: js">let options = { + root: document.querySelector('#scrollArea'), + rootMargin: '0px', + threshold: 1.0 } let observer = new IntersectionObserver(callback, options); </pre> -<p>阈值为1.0意味着目标元素完全出现在root选项指定的元素中可见时,回调函数将会被执行。</p> +<p>阈值为1.0意味着目标元素完全出现在 root 选项指定的元素中可见时,回调函数将会被执行。</p> <h4 id="Intersection_observer_options">Intersection observer options</h4> -<p>传递到{{domxref("IntersectionObserver.IntersectionObserver", "IntersectionObserver()")}}构造函数的 <code>options</code> 对象,允许您控制观察者的回调函数的被调用时的环境。它有以下字段:</p> +<p>传递到 {{domxref("IntersectionObserver.IntersectionObserver", "IntersectionObserver()")}} 构造函数的 <code>options</code> 对象,允许您控制观察者的回调函数的被调用时的环境。它有以下字段:</p> <dl> <dt><code>root</code></dt> <dd>指定根(<strong>root</strong>)元素,用于检查目标的可见性。必须是目标元素的父级元素。如果未指定或者为<code>null</code>,则默认为浏览器视窗。</dd> - <dt><code>rootMargin</code> </dt> - <dd>根(<strong>root</strong>)元素的外边距。类似于 CSS 中的 {{cssxref("margin")}} 属性,比如 "<code>10px 20px 30px 40px"</code> (top, right, bottom, left)。如果有指定root参数,则rootMargin也可以使用百分比来取值。该属性值是用作root元素和target发生交集时候的计算交集的区域范围,使用该属性可以控制root元素每一边的收缩或者扩张。默认值为0。</dd> + <dt><code>rootMargin</code></dt> + <dd>根(<strong>root</strong>)元素的外边距。类似于 CSS 中的 {{cssxref("margin")}} 属性,比如 "<code>10px 20px 30px 40px"</code> (top, right, bottom, left)。如果有指定 root 参数,则 rootMargin 也可以使用百分比来取值。该属性值是用作 root 元素和 target 发生交集时候的计算交集的区域范围,使用该属性可以控制 root 元素每一边的收缩或者扩张。默认值为0。</dd> <dt><code>threshold</code></dt> - <dd>可以是单一的number也可以是number数组,target元素和root元素相交程度达到该值的时候IntersectionObserver注册的回调函数将会被执行。如果你只是想要探测当target元素的在root元素中的可见性超过50%的时候,你可以指定该属性值为0.5。如果你想要target元素在root元素的可见程度每多25%就执行一次回调,那么你可以指定一个数组[0, 0.25, 0.5, 0.75, 1]。默认值是0(意味着只要有一个target像素出现在root元素中,回调函数将会被执行)。该值为1.0含义是当target完全出现在root元素中时候 回调才会被执行。</dd> + <dd>可以是单一的 number 也可以是 number 数组,target 元素和 root 元素相交程度达到该值的时候 IntersectionObserver 注册的回调函数将会被执行。如果你只是想要探测当 target 元素的在 root 元素中的可见性超过50%的时候,你可以指定该属性值为0.5。如果你想要 target 元素在 root 元素的可见程度每多25%就执行一次回调,那么你可以指定一个数组 <code>[0, 0.25, 0.5, 0.75, 1]</code>。默认值是0 (意味着只要有一个 target 像素出现在 root 元素中,回调函数将会被执行)。该值为1.0含义是当 target 完全出现在 root 元素中时候 回调才会被执行。</dd> <dt> <h4 id="Targeting_an_element_to_be_observed">Targeting an element to be observed</h4> </dt> </dl> -<p>为每个观察者配置一个目标</p> +<p>创建一个 observer 后需要给定一个目标元素进行观察。</p> <pre class="brush: js notranslate">let target = document.querySelector('#listItem'); observer.observe(target); </pre> -<p>每当目标满足该IntersectionObserver指定的threshold值,回调被调用。</p> +<p>每当目标满足该 IntersectionObserver 指定的 threshold 值,回调被调用。</p> -<p>只要目标满足为IntersectionObserver指定的阈值,就会调用回调。回调接收 {{domxref("IntersectionObserverEntry")}} 对象和观察者的列表:</p> +<p>只要目标满足为 IntersectionObserver 指定的阈值,就会调用回调。回调接收 {{domxref("IntersectionObserverEntry")}} 对象和观察者的列表:</p> -<pre class="notranslate"><code>let callback =(entries, observer) => { +<pre class="brush: js">let callback =(entries, observer) => { entries.forEach(entry => { - // Each entry describes an intersection change for one observed - // target element: - // entry.boundingClientRect - // entry.intersectionRatio - // entry.intersectionRect - // entry.isIntersecting - // entry.rootBounds - // entry.target - // entry.time + // Each entry describes an intersection change for one observed target element: + // entry.boundingClientRect + // entry.intersectionRatio + // entry.intersectionRect + // entry.isIntersecting + // entry.rootBounds + // entry.target + // entry.time }); -};</code></pre> +};</pre> <p>请留意,你注册的回调函数将会在主线程中被执行。所以该函数执行速度要尽可能的快。如果有一些耗时的操作需要执行,建议使用 {{domxref("Window.requestIdleCallback()")}} 方法。</p> <h3 id="How_intersection_is_calculated_--_交集的计算">How intersection is calculated -- 交集的计算</h3> -<p>所有区域均被Intersection Observer API 当做一个矩形看待。如果元素是不规则的图形也将会被看成一个包含元素所有区域的最小矩形,相似的,如果元素发生的交集部分不是一个矩形,那么也会被看作是一个包含他所有交集区域的最小矩形。</p> +<p>所有区域均被 Intersection Observer API 当做一个矩形看待。如果元素是不规则的图形也将会被看成一个包含元素所有区域的最小矩形,相似的,如果元素发生的交集部分不是一个矩形,那么也会被看作是一个包含他所有交集区域的最小矩形。</p> -<p>这个有助于理解IntersectionObserverEntry 的属性,IntersectionObserverEntry用于描述target和root的交集。</p> +<p>上述解释有助于理解<code>IntersectionObserverEntry</code> 提供的属性,其用于描述 target 和 root 的交集。</p> <h4 id="The_intersection_root_and_root_margin">The intersection root and root margin</h4> -<p>在我们开始跟踪target元素和容器元素之前,我们要先知道什么是容器(root)元素。容器元素又称为<strong>intersection root</strong>, 或 <strong>root element</strong>.这个既可以是target元素祖先元素也可以是指定null则使用浏览器视口做为容器(root)。</p> +<p>在我们开始跟踪 target 元素和容器元素之前,我们要先知道什么是容器 (root) 元素。容器元素又称为 <strong>intersection root</strong>,或 <strong>root element</strong>。 这个既可以是 target 元素祖先元素也可以是指定 null 则使用浏览器视口做为容器(root)。</p> -<p>用作描述intersection root 元素边界的矩形可以使用<strong>root margin</strong>来调整矩形大小,即rootMargin属性,在我们创建IntersectionObserver对象的时候使用。rootMargin的属性值将会做为margin偏移值添加到intersection root 元素的对应的margin位置,并最终形成root 元素的矩形边界。</p> + +<p><strong><dfn>root intersection rectangle</dfn></strong> 是用来对目标元素进行相交检测的矩形,它的大小有以下几种情况:</p> + +<ul> + <li>如果 intersection root 隐含 root (值为<code>null</code>) (也就是顶级 {{domxref("Document")}}), 那么 root intersection 矩形就是视窗的矩形大小。</li> + <li>如果 intersection root 有溢出部分, 则 root intersection 矩形是 root 元素的内容 (content) 区域.</li> + <li>否则,root intersection 矩形就是 intersection root 的 bounding client rectangle (可以调用元素的 {{domxref("Element.getBoundingClientRect", "getBoundingClientRect()")}} 方法获取).</li> +</ul> + +<p>用作描述 intersection root 元素边界的矩形可以使用 <strong>root margin</strong> 来调整矩形大小,即 <code>rootMargin</code> 属性,在我们创建 IntersectionObserver 对象的时候使用。<code>rootMargin</code> 的属性值将会做为 margin 偏移值添加到 intersection root 元素的对应的 margin 位置,并最终形成 root 元素的矩形边界 (在执行回调函数时可由 {{domxref("IntersectionObserverEntry.rootBounds")}} 得到)。</p> <h4 id="Thresholds">Thresholds</h4> -<p>IntersectionObserver API并不会每次在元素的交集发生变化的时候都会执行回调。相反它使用了<strong>thresholds</strong>参数。当你创建一个observer的时候,你可以提供一个或者多个number类型的数值用来表示target元素在root元素的可见程序的百分比,然后,API的回调函数只会在元素达到<strong>thresholds</strong>规定的阈值时才会执行。</p> +<p>IntersectionObserver API 并不会每次在元素的交集发生变化的时候都会执行回调。相反它使用了 <strong>thresholds</strong> 参数。当你创建一个 observer 的时候,你可以提供一个或者多个 number 类型的数值用来表示 target 元素在 root 元素的可见程序的百分比,然后,API的回调函数只会在元素达到 <strong>thresholds</strong> 规定的阈值时才会执行。</p> -<p>例如,当你想要在target在root元素中中的可见性每超过25%或者减少25%的时候都通知一次。你可以在创建observer的时候指定<strong>thresholds</strong>属性值为[0, 0.25, 0.5, 0.75, 1],你可以通过检测在每次交集发生变化的时候的都会传递回调函数的参数"IntersectionObserverEntry.isIntersecting"的属性值来判断target元素在root元素中的可见性是否发生变化。如果isIntersecting 是 true,target元素的至少已经达到<strong>thresholds</strong>属性值当中规定的其中一个阈值,如果是false,target元素不在给定的阈值范围内可见。</p> +<p>例如,当你想要在 target 在 root 元素中中的可见性每超过25%或者减少25%的时候都通知一次。你可以在创建 observer 的时候指定 <strong>thresholds</strong> 属性值为 <code>[0, 0.25, 0.5, 0.75, 1]</code>,你可以通过检测在每次交集发生变化的时候的都会传递回调函数的参数 {{domxref("IntersectionObserverEntry.isIntersecting", "isIntersecting")}} 的属性值来判断 target 元素在 root 元素中的可见性是否发生变化。如果 <code>isIntersecting</code> 为真,target 元素的至少已经达到 <strong>thresholds</strong> 属性值当中规定的其中一个阈值,如果为假,则 target 元素不在给定的阈值范围内可见。</p> -<p>为了让我们感受下thresholds是如何工作的,尝试滚动以下的例子,每一个colored box 的四个边角都会展示自身在root元素中的可见程度百分比,所以在你滚动root的时候你将会看到四个边角的数值一直在发生变化。每一个box都有不同的thresholds:</p> +<p>Note that it's possible to have a non-zero intersection rectangle, which can happen if the intersection is exactly along the boundary between the two or the area of {{domxref("IntersectionObserverEntry.boundingClientRect", "boundingClientRect")}} is zero. This state of the target and root sharing a boundary line is not considered enough to be considered transitioning into an intersecting state.</p> + +<p>为了让我们感受下 thresholds 是如何工作的,尝试滚动以下的例子,每一个 colored box 的四个边角都会展示自身在 root 元素中的可见程度百分比,所以在你滚动 root 的时候你将会看到四个边角的数值一直在发生变化。 每一个 box 都有不同的 thresholds:</p> <ul> - <li>第一个box的thresholds属性值 <code>[0.00, 0.01, 0.02, ..., 0.99, 1.00]</code>.</li> - <li>第二个box只有唯一的值 [0.5].</li> - <li>第三个box thresholds按10%从0递增(0%, 10%, 20%, etc.).</li> - <li>最后一个box [0, 0.25, 0.5, 0.75, 1.0]</li> + <li>第一个盒子的 thresholds 包含每个可视百分比,也就是说,{{domxref("IntersectionObserver.thresholds")}} 数组是 <code>[0.00, 0.01, 0.02, ..., 0.99, 1.00]</code>。</li> + <li>第二个盒子只有唯一的值 <code>[0.5]</code>。</li> + <li>第三个盒子的 thresholds 按10%从0递增(0%, 10%, 20%, etc.)。</li> + <li>最后一个盒子为 <code>[0, 0.25, 0.5, 0.75, 1.0]</code>。</li> </ul> +<div id="Threshold_example"> + +<pre class="brush: html hidden"><template id="boxTemplate"> + <div class="sampleBox"> + <div class="label topLeft"></div> + <div class="label topRight"></div> + <div class="label bottomLeft"></div> + <div class="label bottomRight"></div> + </div> +</template> + +<main> + <div class="contents"> + <div class="wrapper"> + </div> + </div> +</main></pre> + +<pre class="brush: css hidden">.contents { + position: absolute; + width: 700px; + height: 1725px; +} + +.wrapper { + position: relative; + top: 600px; +} + +.sampleBox { + position: relative; + left: 175px; + width: 150px; + background-color: rgb(245, 170, 140); + border: 2px solid rgb(201, 126, 17); + padding: 4px; + margin-bottom: 6px; +} + +#box1 { + height: 200px; +} + +#box2 { + height: 75px; +} + +#box3 { + height: 150px; +} + +#box4 { + height: 100px; +} + +.label { + font: 14px "Open Sans", "Arial", sans-serif; + position: absolute; + margin: 0; + background-color: rgba(255, 255, 255, 0.7); + border: 1px solid rgba(0, 0, 0, 0.7); + width: 3em; + height: 18px; + padding: 2px; + text-align: center; +} + +.topLeft { + left: 2px; + top: 2px; +} + +.topRight { + right: 2px; + top: 2px; +} + +.bottomLeft { + bottom: 2px; + left: 2px; +} + +.bottomRight { + bottom: 2px; + right: 2px; +} +</pre> + +<pre class="brush: js hidden">let observers = []; + +startup = () => { + let wrapper = document.querySelector(".wrapper"); + + // Options for the observers + + let observerOptions = { + root: null, + rootMargin: "0px", + threshold: [] + }; + + // An array of threshold sets for each of the boxes. The + // first box's thresholds are set programmatically + // since there will be so many of them (for each percentage + // point). + + let thresholdSets = [ + [], + [0.5], + [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0], + [0, 0.25, 0.5, 0.75, 1.0] + ]; + + for (let i=0; i<=1.0; i+= 0.01) { + thresholdSets[0].push(i); + } + + // Add each box, creating a new observer for each + + for (let i=0; i<4; i++) { + let template = document.querySelector("#boxTemplate").content.cloneNode(true); + let boxID = "box" + (i+1); + template.querySelector(".sampleBox").id = boxID; + wrapper.appendChild(document.importNode(template, true)); + + // Set up the observer for this box + + observerOptions.threshold = thresholdSets[i]; + observers[i] = new IntersectionObserver(intersectionCallback, observerOptions); + observers[i].observe(document.querySelector("#" + boxID)); + } + + // Scroll to the starting position + + document.scrollingElement.scrollTop = wrapper.firstElementChild.getBoundingClientRect().top + window.scrollY; + document.scrollingElement.scrollLeft = 750; +} + +intersectionCallback = (entries) => { + entries.forEach((entry) => { + let box = entry.target; + let visiblePct = (Math.floor(entry.intersectionRatio * 100)) + "%"; + + box.querySelector(".topLeft").innerHTML = visiblePct; + box.querySelector(".topRight").innerHTML = visiblePct; + box.querySelector(".bottomLeft").innerHTML = visiblePct; + box.querySelector(".bottomRight").innerHTML = visiblePct; + }); +} + +startup(); +</pre> +</div> + +<p>{{EmbedLiveSample("Threshold_example", 500, 500)}}</p> + + + +<h4 id="Clipping_and_the_intersection_rectangle">Clipping and the intersection rectangle</h4> + +<p>The browser computes the final intersection rectangle as follows; this is all done for you, but it can be helpful to understand these steps in order to better grasp exactly when intersections will occur.</p> + +<ol> + <li>The target element's bounding rectangle (that is, the smallest rectangle that fully encloses the bounding boxes of every component that makes up the element) is obtained by calling {{domxref("Element.getBoundingClientRect", "getBoundingClientRect()")}} on the target. This is the largest the intersection rectangle may be. The remaining steps will remove any portions that don't intersect.</li> + <li>Starting at the target's immediate parent block and moving outward, each containing block's clipping (if any) is applied to the intersection rectangle. A block's clipping is determined based on the intersection of the two blocks and the clipping mode (if any) specified by the {{cssxref("overflow")}} property. Setting <code>overflow</code> to anything but <code>visible</code> causes clipping to occur.</li> + <li>If one of the containing elements is the root of a nested browsing context (such as the document contained in an {{HTMLElement("iframe")}}, the intersection rectangle is clipped to the containing context's viewport, and recursion upward through the containers continues with the container's containing block. So if the top level of an <code><iframe></code> is reached, the intersection rectangle is clipped to the frame's viewport, then the frame's parent element is the next block recursed through toward the intersection root.</li> + <li>When recursion upward reaches the intersection root, the resulting rectangle is mapped to the intersection root's coordinate space.</li> + <li>The resulting rectangle is then updated by intersecting it with the <a href="#root-intersection-rectangle">root intersection rectangle</a>.</li> + <li>This rectangle is, finally, mapped to the coordinate space of the target's {{domxref("document")}}.</li> +</ol> + +<h3 id="Intersection_change_callbacks">Intersection change callbacks</h3> + +<p>When the amount of a target element which is visible within the root element crosses one of the visibility thresholds, the {{domxref("IntersectionObserver")}} object's callback is executed. The callback receives as input an array of all of {{domxref("IntersectionObserverEntry")}} objects, one for each threshold which was crossed, and a reference to the <code>IntersectionObserver</code> object itself.</p> + +<p>Each entry in the list of thresholds is an {{domxref("IntersectionObserverEntry")}} object describing one threshold that was crossed; that is, each entry describes how much of a given element is intersecting with the root element, whether or not the element is considered to be intersecting or not, and the direction in which the transition occurred.</p> + +<p>The code snippet below shows a callback which keeps a counter of how many times elements transition from not intersecting the root to intersecting by at least 75%. For a threshold value of 0.0 (default) the callback is called <a href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserverentry-isintersecting">approximately</a> upon transition of the boolean value of {{domxref("IntersectionObserverEntry.isIntersecting", "isIntersecting")}}. The snippet thus first checks that the transition is a positive one, then determines whether {{domxref("IntersectionObserverEntry.intersectionRatio", "intersectionRatio")}} is above 75%, in which case it increments the counter.</p> + +<pre class="brush: js">intersectionCallback(entries) => { + entries.forEach(entry => { + if (entry.isIntersecting) { + let elem = entry.target; + + if (entry.intersectionRatio >= 0.75) { + intersectionCounter++; + } + } + }); +} +</pre> + <h2 id="Interfaces">Interfaces</h2> <dl> <dt>{{domxref("IntersectionObserver")}}</dt> - <dd>Provides a way to <span style="line-height: 1.5;">asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's {{Glossary('viewport')}}. The ancestor or viewport is referred to as the root.</span></dd> + <dd>The primary interface for the Intersection Observer API. Provides methods for creating and managing an observer which can watch any number of target elements for the same intersection configuration. Each observer can asynchronously observe changes in the intersection between one or more target elements and a shared ancestor element or with their top-level {{domxref("Document")}}'s {{Glossary('viewport')}}. The ancestor or viewport is referred to as the <strong>root</strong>.</dd> <dt>{{domxref("IntersectionObserverEntry")}}</dt> - <dd>Provides information about the intersection of a particular target with the observers root element at a particular time. Instances of this interface cannot be created, but a list of them is returned by {{domxref("IntersectionObserver.takeRecords", "IntersectionObserver.takeRecords()")}}.</dd> + <dd>Describes the intersection between the target element and its root container at a specific moment of transition. Objects of this type can only be obtained in two ways: as an input to your <code>IntersectionObserver</code> callback, or by calling {{domxref("IntersectionObserver.takeRecords()")}}.</dd> +</dl> + +<h2 id="A_simple_example">A simple example</h2> + +<p>This simple example causes a target element to change its color and transparency as it becomes more or less visible. At <a href="/en-US/docs/Web/API/Intersection_Observer_API/Timing_element_visibility">Timing element visibility with the Intersection Observer API</a>, you can find a more extensive example showing how to time how long a set of elements (such as ads) are visible to the user and to react to that information by recording statistics or by updating elements..</p> + +<h3 id="HTML">HTML</h3> + +<p>The HTML for this example is very short, with a primary element which is the box that we'll be targeting (with the creative ID <code>"box"</code>) and some contents within the box.</p> + +<pre class="brush: html"><div id="box"> + <div class="vertical"> + Welcome to <strong>The Box!</strong> + </div> +</div></pre> + +<h3 id="CSS">CSS</h3> + +<p>The CSS isn't terribly important for the purposes of this example; it lays out the element and establishes that the {{cssxref("background-color")}} and {{cssxref("border")}} attributes can participate in <a href="/en-US/docs/Web/CSS/CSS_Transitions">CSS transitions</a>, which we'll use to affect the changes to the element as it becomes more or less obscured.</p> + +<pre class="brush: css">#box { + background-color: rgba(40, 40, 190, 255); + border: 4px solid rgb(20, 20, 120); + transition: background-color 1s, border 1s; + width: 350px; + height: 350px; + display: flex; + align-items: center; + justify-content: center; + padding: 20px; +} + +.vertical { + color: white; + font: 32px "Arial"; +} + +.extra { + width: 350px; + height: 350px; + margin-top: 10px; + border: 4px solid rgb(20, 20, 120); + text-align: center; + padding: 20px; +}</pre> + +<h3 id="JavaScript">JavaScript</h3> + +<p>Finally, let's take a look at the JavaScript code that uses the Intersection Observer API to make things happen.</p> + +<h4 id="Setting_up">Setting up</h4> + +<p>First, we need to prepare some variables and install the observer.</p> + +<pre class="brush: js">const numSteps = 20.0; + +let boxElement; +let prevRatio = 0.0; +let increasingColor = "rgba(40, 40, 190, ratio)"; +let decreasingColor = "rgba(190, 40, 40, ratio)"; + +// Set things up +window.addEventListener("load", (event) => { + boxElement = document.querySelector("#box"); + + createObserver(); +}, false);</pre> + +<p>The constants and variables we set up here are:</p> + +<dl> + <dt><code>numSteps</code></dt> + <dd>A constant which indicates how many thresholds we want to have between a visibility ratio of 0.0 and 1.0.</dd> + <dt><code>prevRatio</code></dt> + <dd>This variable will be used to record what the visibility ratio was the last time a threshold was crossed; this will let us figure out whether the target element is becoming more or less visible.</dd> + <dt><code>increasingColor</code></dt> + <dd>A string defining a color we'll apply to the target element when the visibility ratio is increasing. The word "ratio" in this string will be replaced with the target's current visibility ratio, so that the element not only changes color but also becomes increasingly opaque as it becomes less obscured.</dd> + <dt><code>decreasingColor</code></dt> + <dd>Similarly, this is a string defining a color we'll apply when the visibility ratio is decreasing.</dd> </dl> +<p>We call {{domxref("EventTarget.addEventListener", "Window.addEventListener()")}} to start listening for the {{event("load")}} event; once the page has finished loading, we get a reference to the element with the ID <code>"box"</code> using {{domxref("Document.querySelector", "querySelector()")}}, then call the <code>createObserver()</code> method we'll create in a moment to handle building and installing the intersection observer.</p> + +<h4 id="Creating_the_intersection_observer">Creating the intersection observer</h4> + +<p>The <code>createObserver()</code> method is called once page load is complete to handle actually creating the new {{domxref("IntersectionObserver")}} and starting the process of observing the target element.</p> + +<pre class="brush: js">function createObserver() { + let observer; + + let options = { + root: null, + rootMargin: "0px", + threshold: buildThresholdList() + }; + + observer = new IntersectionObserver(handleIntersect, options); + observer.observe(boxElement); +}</pre> + +<p>This begins by setting up an <code>options</code> object containing the settings for the observer. We want to watch for changes in visibility of the target element relative to the document's viewport, so <code>root</code> is <code>null</code>. We need no margin, so the margin offset, <code>rootMargin</code>, is specified as "0px". This causes the observer to watch for changes in the intersection between the target element's bounds and those of the viewport, without any added (or subtracted) space.</p> + +<p>The list of visibility ratio thresholds, <code>threshold</code>, is constructed by the function <code>buildThresholdList()</code>. The threshold list is built programmatically in this example since there are a number of them and the number is intended to be adjustable.</p> + +<p>Once <code>options</code> is ready, we create the new observer, calling the {{domxref("IntersectionObserver.IntersectionObserver", "IntersectionObserver()")}} constructor, specifying a function to be called when intersection crosses one of our thresholds, <code>handleIntersect()</code>, and our set of options. We then call {{domxref("IntersectionObserver.observe", "observe()")}} on the returned observer, passing into it the desired target element.</p> + +<p>We could opt to monitor multiple elements for visibility intersection changes with respect to the viewport by calling <code>observer.observe()</code> for each of those elements, if we wanted to do so.</p> + +<h4 id="Building_the_array_of_threshold_ratios">Building the array of threshold ratios</h4> + +<p>The <code>buildThresholdList()</code> function, which builds the list of thresholds, looks like this:</p> + +<pre class="brush: js">function buildThresholdList() { + let thresholds = []; + let numSteps = 20; + + for (let i=1.0; i<=numSteps; i++) { + let ratio = i/numSteps; + thresholds.push(ratio); + } + + thresholds.push(0); + return thresholds; +}</pre> + +<p>This builds the array of thresholds—each of which is a ratio between 0.0 and 1.0, by pushing the value <code>i/numSteps</code> onto the <code>thresholds</code> array for each integer <code>i</code> between 1 and <code>numSteps</code>. It also pushes 0 to include that value. The result, given the default value of <code>numSteps</code> (20), is the following list of thresholds:</p> + +<table class="standard-table"> + <tbody> + <tr> + <th>#</th> + <th>Ratio</th> + <th>#</th> + <th>Ratio</th> + </tr> + <tr> + <th>1</th> + <td>0.05</td> + <th>11</th> + <td>0.55</td> + </tr> + <tr> + <th>2</th> + <td>0.1</td> + <th>12</th> + <td>0.6</td> + </tr> + <tr> + <th>3</th> + <td>0.15</td> + <th>13</th> + <td>0.65</td> + </tr> + <tr> + <th>4</th> + <td>0.2</td> + <th>14</th> + <td>0.7</td> + </tr> + <tr> + <th>5</th> + <td>0.25</td> + <th>15</th> + <td>0.75</td> + </tr> + <tr> + <th>6</th> + <td>0.3</td> + <th>16</th> + <td>0.8</td> + </tr> + <tr> + <th>7</th> + <td>0.35</td> + <th>17</th> + <td>0.85</td> + </tr> + <tr> + <th>8</th> + <td>0.4</td> + <th>18</th> + <td>0.9</td> + </tr> + <tr> + <th>9</th> + <td>0.45</td> + <th>19</th> + <td>0.95</td> + </tr> + <tr> + <th>10</th> + <td>0.5</td> + <th>20</th> + <td>1.0</td> + </tr> + </tbody> +</table> + +<p>We could, of course, hard-code the array of thresholds into our code, and often that's what you'll end up doing. But this example leaves room for adding configuration controls to adjust the granularity, for example.</p> + +<h4 id="Handling_intersection_changes">Handling intersection changes</h4> + +<p>When the browser detects that the target element (in our case, the one with the ID <code>"box"</code>) has been unveiled or obscured such that its visibility ratio crosses one of the thresholds in our list, it calls our handler function, <code>handleIntersect()</code>:</p> + +<pre class="brush: js">function handleIntersect(entries, observer) { + entries.forEach((entry) => { + if (entry.intersectionRatio > prevRatio) { + entry.target.style.backgroundColor = increasingColor.replace("ratio", entry.intersectionRatio); + } else { + entry.target.style.backgroundColor = decreasingColor.replace("ratio", entry.intersectionRatio); + } + + prevRatio = entry.intersectionRatio; + }); +}</pre> + +<p>For each {{domxref("IntersectionObserverEntry")}} in the list <code>entries</code>, we look to see if the entry's {{domxref("IntersectionObserverEntry.intersectionRatio", "intersectionRatio")}} is going up; if it is, we set the target's {{cssxref("background-color")}} to the string in <code>increasingColor</code> (remember, it's <code>"rgba(40, 40, 190, ratio)"</code>), replaces the word "ratio" with the entry's <code>intersectionRatio</code>. The result: not only does the color get changed, but the transparency of the target element changes, too; as the intersection ratio goes down, the background color's alpha value goes down with it, resulting in an element that's more transparent.</p> + +<p>Similarly, if the <code>intersectionRatio</code> is going down, we use the string <code>decreasingColor</code> and replace the word "ratio" in that with the <code>intersectionRatio</code> before setting the target element's <code>background-color</code>.</p> + +<p>Finally, in order to track whether the intersection ratio is going up or down, we remember the current ratio in the variable <code>prevRatio</code>.</p> + +<h3 id="Result">Result</h3> + +<p>Below is the resulting content. Scroll this page up and down and notice how the appearance of the box changes as you do so.</p> + +<p>{{EmbedLiveSample('A_simple_example', 425, 425)}}</p> + +<p>There's an even more extensive example at <a href="/en-US/docs/Web/API/Intersection_Observer_API/Timing_element_visibility">Timing element visibility with the Intersection Observer API</a>.</p> + + <h2 id="Specifications">Specifications</h2> <table class="standard-table"> @@ -156,12 +594,12 @@ observer.observe(target); <h2 id="浏览器兼容性">浏览器兼容性</h2> - - <p>{{Compat("api.IntersectionObserver")}}</p> <h2 id="更多参考">更多参考</h2> <ul> <li><a href="https://github.com/w3c/IntersectionObserver/blob/master/polyfill/intersection-observer.js">Intersection Observer polyfill</a></li> -</ul> + <li><a href="/zh-CN/docs/Web/API/Intersection_Observer_API/Timing_element_visibility">Timing element visibility with the Intersection Observer API</a></li> + <li>{{domxref("IntersectionObserver")}} and {{domxref("IntersectionObserverEntry")}}</li> +</ul>
\ No newline at end of file diff --git a/files/zh-cn/web/api/intersectionobserver/disconnect/index.html b/files/zh-cn/web/api/intersectionobserver/disconnect/index.html index 0e4513516e..92a8b99ff1 100644 --- a/files/zh-cn/web/api/intersectionobserver/disconnect/index.html +++ b/files/zh-cn/web/api/intersectionobserver/disconnect/index.html @@ -4,6 +4,8 @@ slug: Web/API/IntersectionObserver/disconnect tags: - API - Disconnect + - Intersection Observer + - Intersection Observer API - IntersectionObserver - Method translation_of: Web/API/IntersectionObserver/disconnect @@ -14,7 +16,8 @@ translation_of: Web/API/IntersectionObserver/disconnect <h2 id="语法">语法</h2> -<pre class="syntaxbox"><em>IntersectionObserver</em>.disconnect();</pre> +<pre + class="brush: js"><em>intersectionObserver</em>.disconnect();</pre> <h3 id="参数">参数</h3> @@ -26,26 +29,13 @@ translation_of: Web/API/IntersectionObserver/disconnect <h2 id="说明">说明</h2> -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Specification</th> - <th scope="col">Status</th> - <th scope="col">Comment</th> - </tr> - <tr> - <td>{{SpecName('IntersectionObserver','#dom-intersectionobserver-disconnect','IntersectionObserver.disconnect()')}}</td> - <td>{{Spec2('IntersectionObserver')}}</td> - <td>Initial definition.</td> - </tr> - </tbody> -</table> +{{Specifications}} <h2 id="浏览器兼容性">浏览器兼容性 </h2> -<p>{{Compat("api.IntersectionObserver.disconnect")}}</p> +<p>{{Compat}}</p> -<h2 id="See_also">See also</h2> +<h2 id="See_also">参考</h2> <ul> <li>{{domxref("IntersectionObserver.observe", "observe()")}}</li> diff --git a/files/zh-cn/web/api/intersectionobserver/index.html b/files/zh-cn/web/api/intersectionobserver/index.html index e3bcf96bd3..1aa608a771 100644 --- a/files/zh-cn/web/api/intersectionobserver/index.html +++ b/files/zh-cn/web/api/intersectionobserver/index.html @@ -47,11 +47,12 @@ translation_of: Web/API/IntersectionObserver <dt>{{domxref("IntersectionObserver.unobserve()")}}</dt> <dd><font face="Open Sans, arial, x-locale-body, sans-serif">使</font><code>IntersectionObserver</code>停止监听特定目标元素。</dd> <dt> - <h2 id="示例">示例</h2> </dt> </dl> -<pre><code>var intersectionObserver = new IntersectionObserver(function(entries) { +<h2 id="示例">示例</h2> + +<pre class="brush: js">var intersectionObserver = new IntersectionObserver(function(entries) { // If intersectionRatio is 0, the target is out of view // and we do not need to do anything. if (entries[0].intersectionRatio <= 0) return; @@ -60,28 +61,15 @@ translation_of: Web/API/IntersectionObserver console.log('Loaded new items'); }); // start observing -intersectionObserver.observe(document.querySelector('.scrollerFooter'));</code></pre> +intersectionObserver.observe(document.querySelector('.scrollerFooter'));</pre> <h2 id="规范">规范</h2> -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">规范</th> - <th scope="col">状态</th> - <th scope="col">备注</th> - </tr> - <tr> - <td>{{SpecName("IntersectionObserver", "#intersection-observer-interface", "IntersectionObserver")}}</td> - <td>{{Spec2('IntersectionObserver')}}</td> - <td> </td> - </tr> - </tbody> -</table> +{{Specifications}} <h2 id="浏览器兼容">浏览器兼容</h2> -<p>{{Compat("api.IntersectionObserver")}}</p> +<p>{{Compat}}</p> <h2 id="参考">参考</h2> diff --git a/files/zh-cn/web/api/intersectionobserver/observe/index.html b/files/zh-cn/web/api/intersectionobserver/observe/index.html index c2758917f6..9e5a3a577a 100644 --- a/files/zh-cn/web/api/intersectionobserver/observe/index.html +++ b/files/zh-cn/web/api/intersectionobserver/observe/index.html @@ -11,7 +11,8 @@ translation_of: Web/API/IntersectionObserver/observe <h2 id="语法">语法</h2> -<pre class="syntaxbox"><em>IntersectionObserver</em>.observe(<em>targetElement</em>);</pre> +<pre + class="brush: js"><em>IntersectionObserver</em>.observe(<em>targetElement</em>);</pre> <h3 id="参数">参数</h3> @@ -25,37 +26,41 @@ translation_of: Web/API/IntersectionObserver/observe <p><code>undefined</code>.</p> -<dl> -</dl> +<h2 id="Examples">示例</h2> + +<pre class="brush: js"> +// Register IntersectionObserver +const io = new IntersectionObserver(entries => { + entries.forEach(entry => { + // Add 'active' class if observation target is inside viewport + if (entry.intersectionRatio > 0) { + entry.target.classList.add('active'); + } + // Remove 'active' class otherwise + else { + entry.target.classList.remove('active'); + } + }) +}) + +// Declares what to observe, and observes its properties. +const boxElList = document.querySelectorAll('.box'); +boxElList.forEach((el) => { + io.observe(el); +}) +</pre> -<h2 id="Examples">Examples</h2> +<h2 id="Specifications">规范</h2> -<p><<<...>>></p> +{{Specifications}} -<h2 id="Specifications">Specifications</h2> -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Specification</th> - <th scope="col">Status</th> - <th scope="col">Comment</th> - </tr> - <tr> - <td>{{SpecName('IntersectionObserver','#dom-intersectionobserver-observe','IntersectionObserver.observe()')}}</td> - <td>{{Spec2('IntersectionObserver')}}</td> - <td>Initial definition.</td> - </tr> - </tbody> -</table> +<h2 id="Browser_compatibility">浏览器兼容性</h2> -<div> -<h2 id="Browser_compatibility">Browser compatibility</h2> +<p>{{Compat}}</p> -<p>{{Compat("api.IntersectionObserver.observe")}}</p> -</div> -<h2 id="See_also">See also</h2> +<h2 id="See_also">参考</h2> <ul> <li>{{domxref("IntersectionObserver.unobserve()")}}</li> diff --git a/files/zh-cn/web/api/intersectionobserver/unobserve/index.html b/files/zh-cn/web/api/intersectionobserver/unobserve/index.html index c3099ca224..45f8e1f5b5 100644 --- a/files/zh-cn/web/api/intersectionobserver/unobserve/index.html +++ b/files/zh-cn/web/api/intersectionobserver/unobserve/index.html @@ -4,14 +4,23 @@ slug: Web/API/IntersectionObserver/unobserve tags: - API - Intersection + - Intersection Observer + - Intersection Observer API + - IntersectionObserver - unobserve translation_of: Web/API/IntersectionObserver/unobserve +browser-compat: api.IntersectionObserver.unobserve --- -<div><code>unobserve()方法命令</code>IntersectionObserver停止对一个元素的观察。</div> +<div>{{APIRef("Intersection Observer API")}}</div> + +<p>{{domxref("IntersectionObserver")}}的<code>unobserve()</code> + 方法命令<code>IntersectionObserver</code>停止对一个元素的观察。 +</p> <h2 id="语法">语法</h2> -<pre class="syntaxbox">IntersectionObserver.unobserve(<em>target</em>);</pre> +<pre + class="brush: js"><em>IntersectionObserver</em>.unobserve(<em>target</em>);</pre> <h3 id="参数">参数</h3> @@ -40,24 +49,11 @@ observer.unobserve(document.getElementById("elementToObserve"));</pre> <h2 id="说明">说明</h2> -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Specification</th> - <th scope="col">Status</th> - <th scope="col">Comment</th> - </tr> - <tr> - <td>{{SpecName('IntersectionObserver','#dom-intersectionobserver-unobserve','IntersectionObserver.unobserve()')}}</td> - <td>{{Spec2('IntersectionObserver')}}</td> - <td>Initial definition.</td> - </tr> - </tbody> -</table> +{{Specifications}} <h2 id="浏览器兼容性">浏览器兼容性</h2> -<p>{{Compat("api.IntersectionObserver.unobserve")}}</p> +<p>{{Compat}}</p> <h2 id="另请参阅">另请参阅</h2> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/issecurecontext/index.html b/files/zh-cn/web/api/issecurecontext/index.html index d9f72b13a5..355b4dcde6 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/issecurecontext/index.html +++ b/files/zh-cn/web/api/issecurecontext/index.html @@ -1,6 +1,6 @@ --- title: WindowOrWorkerGlobalScope.isSecureContext -slug: Web/API/WindowOrWorkerGlobalScope/isSecureContext +slug: Web/API/isSecureContext tags: - API - Property @@ -10,6 +10,7 @@ tags: - Workers - isSecureContext translation_of: Web/API/WindowOrWorkerGlobalScope/isSecureContext +original_slug: Web/API/WindowOrWorkerGlobalScope/isSecureContext --- <p>{{APIRef()}}{{SeeCompatTable}}</p> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/origin/index.html b/files/zh-cn/web/api/origin/index.html index eac425fbd9..eeb79e7cf3 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/origin/index.html +++ b/files/zh-cn/web/api/origin/index.html @@ -1,11 +1,12 @@ --- title: WindowOrWorkerGlobalScope.origin -slug: Web/API/WindowOrWorkerGlobalScope/origin +slug: Web/API/origin tags: - global scope - origin - serialized translation_of: Web/API/WindowOrWorkerGlobalScope/origin +original_slug: Web/API/WindowOrWorkerGlobalScope/origin --- <p>{{APIRef()}}{{SeeCompatTable}}{{domxref("WindowOrWorkerGlobalScope")}} 接口的 <code><strong>origin</strong></code> 只读属性返回全局范围的 <strong>origin</strong>, 序列化为一个字符串。</p> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/queuemicrotask/index.html b/files/zh-cn/web/api/queuemicrotask/index.html index 3f81e04235..9079205222 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/queuemicrotask/index.html +++ b/files/zh-cn/web/api/queuemicrotask/index.html @@ -1,6 +1,6 @@ --- title: WindowOrWorkerGlobalScope.queueMicrotask() -slug: Web/API/WindowOrWorkerGlobalScope/queueMicrotask +slug: Web/API/queueMicrotask tags: - API - JavaScript @@ -10,6 +10,7 @@ tags: - 同步 - 方法 translation_of: Web/API/WindowOrWorkerGlobalScope/queueMicrotask +original_slug: Web/API/WindowOrWorkerGlobalScope/queueMicrotask --- <div>{{APIRef("HTML DOM")}}</div> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/setinterval/index.html b/files/zh-cn/web/api/setinterval/index.html index d5a453b133..f3401fc5a5 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/setinterval/index.html +++ b/files/zh-cn/web/api/setinterval/index.html @@ -1,6 +1,6 @@ --- title: window.setInterval -slug: Web/API/WindowOrWorkerGlobalScope/setInterval +slug: Web/API/setInterval tags: - API - DOM @@ -8,7 +8,7 @@ tags: - 方法 - 时间 translation_of: Web/API/WindowOrWorkerGlobalScope/setInterval -original_slug: Web/API/Window/setInterval +original_slug: Web/API/WindowOrWorkerGlobalScope/setInterval --- <div>{{ ApiRef("HTML DOM") }}</div> diff --git a/files/zh-cn/web/api/windoworworkerglobalscope/settimeout/index.html b/files/zh-cn/web/api/settimeout/index.html index d0c80bb95b..1499eccade 100644 --- a/files/zh-cn/web/api/windoworworkerglobalscope/settimeout/index.html +++ b/files/zh-cn/web/api/settimeout/index.html @@ -1,13 +1,13 @@ --- title: window.setTimeout -slug: Web/API/WindowOrWorkerGlobalScope/setTimeout +slug: Web/API/setTimeout tags: - Timers - WindowOrWorkerGlobalScope - WindowTimers - setTimeout translation_of: Web/API/WindowOrWorkerGlobalScope/setTimeout -original_slug: Web/API/Window/setTimeout +original_slug: Web/API/WindowOrWorkerGlobalScope/setTimeout --- <p>{{APIRef("HTML DOM")}}</p> diff --git a/files/zh-cn/web/api/subtlecrypto/index.html b/files/zh-cn/web/api/subtlecrypto/index.html index 24dec9a283..c173e906e8 100644 --- a/files/zh-cn/web/api/subtlecrypto/index.html +++ b/files/zh-cn/web/api/subtlecrypto/index.html @@ -28,8 +28,8 @@ translation_of: Web/API/SubtleCrypto <p>这些函数你可以用来实现系统中的隐私和身份验证等安全功能。<strong>SubtleCrypto </strong>API提供了如下加密函数:</p> <p>* <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign">sign()</a></code> 、 <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/verify">verify()</a></code>: 创建和验证数字签名。<br> - * <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt">encrypt()</a></code> and <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt">decrypt()</a></code>: 加密和解密数据。<br> - * <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest">digest()</a></code>: create a fixed-length, collision-resistant digest of some data.</p> + * <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt">encrypt()</a></code> 和 <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt">decrypt()</a></code>: 加密和解密数据。<br> + * <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest">digest()</a></code>:为数据生成一个定长的,防碰撞的消息摘要。</p> <h3 id="密钥管理功能">密钥管理功能</h3> @@ -232,9 +232,9 @@ translation_of: Web/API/SubtleCrypto <dl> <dt>{{domxref("SubtleCrypto.encrypt()")}}</dt> - <dd><code>以算法、密钥、</code>明文<code>为参数,返回一个包含加密数据的 </code>{{jsxref("Promise")}}对象。</dd> + <dd>以算法、密钥、明文为参数,返回一个包含加密数据的 {{jsxref("Promise")}} 对象。</dd> <dt>{{domxref("SubtleCrypto.decrypt()")}}</dt> - <dd><code>以算法、密钥、</code>明文<code>为参数,返回一个包含解密数据的 </code>{{jsxref("Promise")}}对象。</dd> + <dd>以算法、密钥、密文为参数,返回一个包含解密数据的 {{jsxref("Promise")}} 对象。</dd> <dt>{{domxref("SubtleCrypto.sign()")}}</dt> <dd>以文本、算法和密码为参数,返回一个包含签名的 {{jsxref("Promise")}}。</dd> <dt>{{domxref("SubtleCrypto.verify()")}}</dt> diff --git a/files/zh-cn/web/api/uievent/pagex/index.html b/files/zh-cn/web/api/uievent/pagex/index.html deleted file mode 100644 index b4a7f812f7..0000000000 --- a/files/zh-cn/web/api/uievent/pagex/index.html +++ /dev/null @@ -1,105 +0,0 @@ ---- -title: UIEvent.pageX -slug: Web/API/UIEvent/pageX -translation_of: Web/API/UIEvent/pageX ---- -<p>{{APIRef("DOM Events")}} {{Non-standard_header}}</p> - -<p><code><strong>UIEvent.pageX</strong></code> 是只读属性,它返回相对于整个文档的水平坐标。</p> - -<h2 id="Syntax" name="Syntax">语法</h2> - -<pre class="syntaxbox">var <em>pos</em> = <em>event</em>.pageX</pre> - -<h2 id="Syntax" name="Syntax">示例</h2> - -<pre><em>var pageX</em> = <em>event</em>.pageX;</pre> - -<p><code><em>pageX</em></code> 是鼠标事件触发时,鼠标指针相对于整个文档 X 坐标上像素点的整数值。这一属性同时也参照了页面的水平滚动距离。</p> - -<pre class="brush: js"><html> -<head> -<title>pageX\pageY & layerX\layerY example</title> - -<script type="text/javascript"> - -function showCoords(evt){ - var form = document.forms.form_coords; - var parent_id = evt.target.parentNode.id; - form.parentId.value = parent_id; - form.pageXCoords.value = evt.pageX; - form.pageYCoords.value = evt.pageY; - form.layerXCoords.value = evt.layerX; - form.layerYCoords.value = evt.layerY; -} - -</script> - -<style type="text/css"> - - #d1 { - border: solid blue 1px; - padding: 20px; - } - - #d2 { - position: absolute; - top: 180px; - left: 80%; - right:auto; - width: 40%; - border: solid blue 1px; - padding: 20px; - } - - #d3 { - position: absolute; - top: 240px; - left: 20%; - width: 50%; - border: solid blue 1px; - padding: 10px; - } - -</style> -</head> - -<body onmousedown="showCoords(event)"> - -<p>To display the mouse coordinates please click anywhere on the page.</p> - -<div id="d1"> -<span>This is an un-positioned div so clicking it will return -layerX/layerY values almost the same as pageX/PageY values.</span> -</div> - -<div id="d2"> -<span>This is a positioned div so clicking it will return layerX/layerY -values that are relative to the top-left corner of this positioned -element. Note the pageX\pageY properties still return the -absolute position in the document, including page scrolling.</span> - -<span>Make the page scroll more! This is a positioned div so clicking it -will return layerX/layerY values that are relative to the top-left -corner of this positioned element. Note the pageX\pageY properties still -return the absolute position in the document, including page -scrolling.</span> -</div> - -<div id="d3"> -<form name="form_coords"> - Parent Element id: <input type="text" name="parentId" size="7" /><br /> - pageX:<input type="text" name="pageXCoords" size="7" /> - pageY:<input type="text" name="pageYCoords" size="7" /><br /> - layerX:<input type="text" name="layerXCoords" size="7" /> - layerY:<input type="text" name="layerYCoords" size="7" /> -</form> -</div> - -</body> -</html> -</pre> - -<h2 id="Specification" name="Specification">规范</h2> - -<p>这一属性不在规范中。</p> diff --git a/files/zh-cn/web/api/uievent/pagey/index.html b/files/zh-cn/web/api/uievent/pagey/index.html deleted file mode 100644 index 6c504d3f52..0000000000 --- a/files/zh-cn/web/api/uievent/pagey/index.html +++ /dev/null @@ -1,89 +0,0 @@ ---- -title: event.pageY -slug: Web/API/UIEvent/pageY -translation_of: Web/API/UIEvent/pageY ---- -<p>{{ ApiRef() }}</p> -<h3 id="Summary" name="Summary">概述</h3> -<p>返回事件发生时相对于整个文档的纵坐标.</p> -<h3 id="Syntax" name="Syntax">语法</h3> -<pre class="eval"><em>var pageY</em> = <em>event</em>.pageY; -</pre> -<p><code>pageY</code> 是事件发生时鼠标指针所在位置相对于整个文档的纵坐标,单位为像素.该属性会考虑到页面滚动条的高度.</p> -<h3 id="Example" name="Example">例子</h3> -<pre><html> -<head> -<title>pageX\pageY & layerX\layerY example</title> - -<script type="text/javascript"> - -function showCoords(evt){ - var form = document.forms.form_coords; - var parent_id = evt.target.parentNode.id; - form.parentId.value = parent_id; - form.pageXCoords.value = evt.pageX; - form.pageYCoords.value = evt.pageY; - form.layerXCoords.value = evt.layerX; - form.layerYCoords.value = evt.layerY; -} - -</script> - -<style type="text/css"> - - #d1 { - border: solid blue 1px; - padding: 20px; - } - - #d2 { - position: absolute; - top: 180px; - left: 80%; - right:auto; - width: 40%; - border: solid blue 1px; - padding: 20px; - } - - #d3 { - position: absolute; - top: 240px; - left: 20%; - width: 50%; - border: solid blue 1px; - padding: 10px; - } - -</style> -</head> - -<body onmousedown="showCoords(event)"> - -<p>要显示鼠标所在位置的坐标,请点击页面上的任意地方.</p> - -<div id="d1"> -<span>这是一个未定位的div(没有指定css position属性),所以点击它返回的layerX/layerY值几乎和pageX/PageY值完全相同.</span> -</div> - -<div id="d2"> -<span><span id="result_box" lang="zh-CN"><span>这是一个</span><span>已定位的div</span><span>,因此</span><span>点击它</span><span>时返回</span><span>的layerX</span><span>/</span><span class="hps">layerY的值是相对于自己左上角(</span></span>top-left<span id="result_box" lang="zh-CN"><span class="hps">)位置的坐标值.</span> -<span>然而</span><span>pageX</span><span>/</span><span class="hps">pageY</span><span>属性</span><span>仍</span><span>返回该div在</span><span>文档中</span><span>的</span><span>绝对位置</span><span>,</span><span>包括页面</span><span>滚动</span></span>高度.</span> -</div> - -<div id="d3"> -<form name="form_coords"> - 父元素id: <input type="text" name="parentId" size="7" /><br /> - pageX:<input type="text" name="pageXCoords" size="7" /> - pageY:<input type="text" name="pageYCoords" size="7" /><br /> - layerX:<input type="text" name="layerXCoords" size="7" /> - layerY:<input type="text" name="layerYCoords" size="7" /> -</form> -</div> - -</body> -</html> -</pre> -<h3 id="Specification" name="Specification">规范</h3> -<p>不属于任何公开的标准.</p> -<p>{{ languages( {"pl": "pl/DOM/event.pageY","en": "en/DOM/event.pageY" } ) }}</p> diff --git a/files/zh-cn/web/api/web_audio_api/index.html b/files/zh-cn/web/api/web_audio_api/index.html index f4f25afdfa..75edbde9bf 100644 --- a/files/zh-cn/web/api/web_audio_api/index.html +++ b/files/zh-cn/web/api/web_audio_api/index.html @@ -443,7 +443,7 @@ function voiceMute() { // toggle to mute and unmute sound <li><a href="https://github.com/goldfire/howler.js/">howler.js</a>: a JS audio library that defaults to <a href="https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html">Web Audio API</a> and falls back to <a href="http://www.whatwg.org/specs/web-apps/current-work/#the-audio-element">HTML5 Audio</a>, as well as providing other useful features.</li> </ul> -<section id="Quick_Links"> +<section id="Quick_links"> <h3 id="Quicklinks">Quicklinks</h3> <ol> diff --git a/files/zh-cn/web/api/webrtc_api/connectivity/index.html b/files/zh-cn/web/api/webrtc_api/connectivity/index.html index 647cce7449..7a9950be14 100644 --- a/files/zh-cn/web/api/webrtc_api/connectivity/index.html +++ b/files/zh-cn/web/api/webrtc_api/connectivity/index.html @@ -41,33 +41,33 @@ translation_of: Web/API/WebRTC_API/Connectivity <ol> <li>呼叫者通过 {{domxref("navigator.mediaDevices.getUserMedia()")}} 捕捉本地媒体。</li> <li>呼叫者创建一个<code>RTCPeerConnection</code> 并调用 {{domxref("RTCPeerConnection.addTrack()")}} (注: <code>addStream</code> 已经过时。)</li> - <li>呼叫者调用 ("RTCPeerConnection.createOffer()")来创建一个提议(offer).</li> - <li>呼叫者调用 ("RTCPeerConnection.setLocalDescription()") 将提议(Offer)<em> </em>设置为本地描述 (即,连接的本地描述).</li> + <li>呼叫者调用 {{domxref("RTCPeerConnection.createOffer()")}} 来创建一个提议(offer).</li> + <li>呼叫者调用 {{domxref("RTCPeerConnection.setLocalDescription()")}} 将提议(Offer)<em> </em>设置为本地描述 (即,连接的本地描述).</li> <li>setLocalDescription()之后, 呼叫者请求 STUN 服务创建ice候选(ice candidates)</li> <li>呼叫者通过信令服务器将提议(offer)传递至 本次呼叫的预期的接受者.</li> - <li>接受者收到了提议(offer) 并调用 ("RTCPeerConnection.setRemoteDescription()") 将其记录为远程描述 (也就是连接的另一端的描述).</li> + <li>接受者收到了提议(offer) 并调用 {{domxref("RTCPeerConnection.setRemoteDescription()")}} 将其记录为远程描述 (也就是连接的另一端的描述).</li> <li>接受者做一些可能需要的步骤结束本次呼叫:捕获本地媒体,然后通过{{domxref("RTCPeerConnection.addTrack()")}}添加到连接中。</li> - <li>接受者通过("RTCPeerConnection.createAnswer()")创建一个应答。</li> - <li>接受者调用 ("RTCPeerConnection.setLocalDescription()") 将应答(answer)<em> </em>设置为本地描述. 此时,接受者已经获知连接双方的配置了.</li> + <li>接受者通过 {{domxref("RTCPeerConnection.createAnswer()")}} 创建一个应答。</li> + <li>接受者调用 {{domxref("RTCPeerConnection.setLocalDescription()")}} 将应答(answer)<em> </em>设置为本地描述. 此时,接受者已经获知连接双方的配置了.</li> <li>接受者通过信令服务器将应答传递到呼叫者.</li> <li>呼叫者接受到应答.</li> - <li>呼叫者调用 ("RTCPeerConnection.setRemoteDescription()") 将应答设定为远程描述. 如此,呼叫者已经获知连接双方的配置了.</li> + <li>呼叫者调用 {{domxref("RTCPeerConnection.setRemoteDescription()")}} 将应答设定为远程描述. 如此,呼叫者已经获知连接双方的配置了.</li> </ol> <h3 id="待定的和当前描述"><strong>待定的和当前描述</strong></h3> <p>进一步了解该过程,我们发现localDescription和remoteDescription(返回这两个描述的属性 )并不像外观那样简单。 因为在重新协商期间,提议可能会被拒绝,因为它提出了不兼容的格式,每个端点都有能力提出一种新的格式,但是实际上不会切换到另一个对等体,直到它被其他对等体接受为止。 因此,WebRTC使用待定和当前的描述。</p> -<p><strong>当前描述</strong>(由 ("RTCPeerConnection.currentLocalDescription") 和 ("RTCPeerConnection.currentRemoteDescription") 属性返回 )表示连接实际使用的描述。 这是双方已经完全同意使用的最新连接。</p> +<p><strong>当前描述</strong>(由 {{domxref("RTCPeerConnection.currentLocalDescription")}} 和 {{domxref("RTCPeerConnection.currentRemoteDescription")}} 属性返回 )表示连接实际使用的描述。 这是双方已经完全同意使用的最新连接。</p> -<p><strong>待定的描述</strong>(由 ("RTCPeerConnection.pendingLocalDescription" ) 和 ("RTCPeerConnection.pendingRemoteDescription") 返回 )表示当 分别调用setLocalDescription( )或setRemoteDescription( )。</p> +<p><strong>待定的描述</strong>(由 {{domxref("RTCPeerConnection.pendingLocalDescription")}} 和 {{domxref("RTCPeerConnection.pendingRemoteDescription")}} 返回 )表示当 分别调用setLocalDescription( )或setRemoteDescription( )。</p> -<p>当读取描述( ("RTCPeerConnection.localDescription" ) 和 ("RTCPeerConnection.remoteDescription" ) )返回时,返回的值是pendingLocalDescription / pendingRemoteDescription的值,如果有待处理的描述( 也就是说,待处理描述不为null ); 否则,返回当前描述(currentLocalDescription / currentRemoteDescription )。</p> +<p>当读取描述( {{domxref("RTCPeerConnection.localDescription")}} 和 {{domxref("RTCPeerConnection.remoteDescription")}} )返回时,返回的值是pendingLocalDescription / pendingRemoteDescription的值,如果有待处理的描述( 也就是说,待处理描述不为null ); 否则,返回当前描述(currentLocalDescription / currentRemoteDescription )。</p> <p>通过调用setLocalDescription( )或setRemoteDescription( )更改描述时,将指定的描述设置为待定描述,WebRTC层开始评估是否可以接受。 一旦建议的描述已经达成一致,currentLocalDescription或currentRemoteDescription的值将更改为待处理描述,并且待处理的描述再次设置为null,表示没有待处理的描述。</p> <div class="note"> -<p>pendingLocalDescription不仅包含正在考虑的提议或答案,而且自从提议或应答以来已经收集到的任何本地ICE候选人都被创建。 类似地,pendingRemoteDescription包括通过调用 ("RTCPeerConnection.addIceCandidate( )" ) 提供的任何远程ICE候选。</p> +<p>pendingLocalDescription不仅包含正在考虑的提议或答案,而且自从提议或应答以来已经收集到的任何本地ICE候选人都被创建。 类似地,pendingRemoteDescription包括通过调用 {{domxref("RTCPeerConnection.addIceCandidate()")}} 提供的任何远程ICE候选。</p> </div> <p>有关这些属性和方法的更多细节,请参阅各个文章。</p> diff --git a/files/zh-cn/web/api/xmlhttprequest/responsetype/index.html b/files/zh-cn/web/api/xmlhttprequest/responsetype/index.html index c270b2f109..7201946aae 100644 --- a/files/zh-cn/web/api/xmlhttprequest/responsetype/index.html +++ b/files/zh-cn/web/api/xmlhttprequest/responsetype/index.html @@ -2,44 +2,88 @@ title: XMLHttpRequest.responseType slug: Web/API/XMLHttpRequest/responseType tags: - - XMLHttpRequest.responseType + - AJAX + - API + - HTTP + - HTTP Response + - HTTP Response Type + - Property + - Reference + - Response + - XHR + - XMLHttpRequest + - responseType translation_of: Web/API/XMLHttpRequest/responseType +browser-compat: api.XMLHttpRequest.responseType --- -<p>{{APIRef('XMLHttpRequest')}}</p> +<div>{{APIRef('XMLHttpRequest')}}</div> -<p><strong>XMLHttpRequest.responseType </strong>属性是一个枚举类型的属性,返回响应数据的类型。它允许我们手动的设置返回数据的类型。如果我们将它设置为一个空字符串,它将使用默认的"text"类型。</p> +<p>{{DOMxRef("XMLHttpRequest")}} 属性 <strong><code>responseType</code></strong> 是一个枚举字符串值,用于指定响应中包含的数据类型。</p> -<p>在工作环境(Work Environment)中将responseType的值设置为"document"通常会被忽略. 当将responseType设置为一个特定的类型时,你需要确保服务器所返回的类型和你所设置的返回值类型是兼容的。那么如果两者类型不兼容呢?恭喜你,你会发现服务器返回的数据变成了null,即使服务器返回了数据。还有一个要注意的是,给一个同步请求设置responseType会抛出一个<code>InvalidAccessError</code> 的异常。</p> +<p>它还允许作者更改响应类型。如果将 <code>responseType</code> 的值设置为空字符串,则会使用 <code>text</code> 作为默认值。</p> -<p>responseType支持以下几种值:</p> +<h2 id="syntax">语法</h2> -<p>{{page("/zh-CN/docs/Web/API/XMLHttpRequestResponseType", "取值")}}</p> +<pre class="brush: js">var <var>type</var> = <var>XMLHttpRequest</var>.responseType; -<h2 id="规范">规范</h2> +<var>XMLHttpRequest</var>.responseType = <var>type</var>;</pre> -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">规范</th> - <th scope="col">状态</th> - <th scope="col">注释</th> - </tr> - <tr> - <td>{{SpecName('XMLHttpRequest', '#the-responsetype-attribute')}}</td> - <td>{{Spec2('XMLHttpRequest')}}</td> - <td>WHATWG living standard</td> - </tr> - </tbody> -</table> +<h3 id="Value">值</h3> -<h2 id="浏览器兼容性">浏览器兼容性</h2> +<p>{{JSxRef("Global_Objects/String")}} 类型的值,指定响应包含的数据类型。它可以采用以下值:</p> -<p>{{Compat("api.XMLHttpRequest.responseType")}}</p> +<dl> + <dt><code>""</code></dt> + <dd>空的 <code>responseType</code> 字符串与默认类型 <code>"text"</code> 相同。</dd> + <dt><code>"arraybuffer"</code></dt> + <dd>{{DOMxRef("XMLHttpRequest.response", "response")}} 是一个包含二进制数据的 JavaScript {{JSxRef("ArrayBuffer")}}。</dd> + <dt><code>"blob"</code></dt> + <dd>{{DOMxRef("XMLHttpRequest.response", "response")}} 是一个包含二进制数据的 {{DOMxRef("Blob")}} 对象。</dd> + <dt><code>"document"</code></dt> + <dd>{{DOMxRef("XMLHttpRequest.response", "response")}} 是一个 {{Glossary("HTML")}} {{DOMxRef("Document")}} 或 {{Glossary("XML")}} {{DOMxRef("XMLDocument")}},根据接收到的数据的 MIME 类型而定。请参阅 <a href="/zh-CN/docs/Web/API/XMLHttpRequest/HTML_in_XMLHttpRequest">HTML in XMLHttpRequest</a> 中的 HTML,了解有关使用 XHR 获取 HTML 内容的更多信息。</dd> + <dt><code>"json"</code></dt> + <dd>{{DOMxRef("XMLHttpRequest.response", "response")}} 是通过将接收到的数据内容解析为 {{Glossary("JSON")}} 而创建的 JavaScript 对象。</dd> + <dt><code>"text"</code></dt> + <dd>{{DOMxRef("XMLHttpRequest.response", "response")}} 是 {{DOMxRef("DOMString")}} 对象中的文本。</dd> + <dt><code>"ms-stream"</code> {{non-standard_inline}}</dt> + <dd>{{DOMxRef("XMLHttpRequest.response", "response")}} 是流式下载的一部分;此响应类型仅允许用于下载请求,并且仅受 Internet Explorer 支持。</dd> +</dl> -<h2 id="了解更多">了解更多</h2> +<div class="note"> + <p>将 <code>responseType</code> 设置为特定值时,作者应确保服务器实际发送的响应与该格式兼容。如果服务器返回的数据与设置的 <code>responseType</code> 不兼容,则 {{DOMxRef("XMLHttpRequest.response", "response")}} 的值将为<code>null</code> .</p> +</div> + +<h3 id="Exceptions">异常</h3> + +<dl> + <dt><code>InvalidAccessError</code></dt> + <dd>试图更改 <code>XMLHttpRequest</code> 的 <code>responseType</code>,该值处于同步模式但不在 {{DOMxRef("Worker")}} 中。有关其他详细信息,请参阅下面的 {{anch("Synchronous_XHR_restrictions", "同步 XHR 限制")}}。</dd> +</dl> + +<h2 id="Usage_notes">使用说明</h2> + +<h3 id="Synchronous_XHR_restrictions">同步 XHR 限制</h3> + +<p>您不能在同步<code>XMLHttpRequest</code> 中更改 <code>responseType</code> 的值,除非请求属于 {{DOMxRef("Worker")}}。此限制部分旨在帮助确保同步操作不会用于阻塞浏览器主线程的大型事务,从而阻碍用户体验。</p> + +<p>XHR 请求默认是异步的;它们仅通过在调用 {{DOMxRef("XMLHttpRequest.open", "open()")}} 时将 <code>false</code> 作为可选<code>async</code> 参数的值传递来置于同步模式。</p> + +<h3 id="Restrictions_in_Workers">Worker 中的限制</h3> + +<p>在 {{DOMxRef("Worker")}} 中尝试将 <code>responseType</code> 的值设置为 <code>document</code> 时会被忽略。</p> + +<h2 id="Specifications">规范</h2> + +{{Specifications}} + +<h2 id="Browser_compatibility">浏览器兼容性</h2> + +<p>{{Compat}}</p> + +<h2 id="See_also">相关链接</h2> <ul> - <li><a href="/zh-CN/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest">Using XMLHttpRequest</a></li> - <li><a href="/zh-CN/docs/Web/API/XMLHttpRequest/HTML_in_XMLHttpRequest">HTML in XMLHttpRequest</a></li> - <li>响应数据: {{domxref("XMLHttpRequest.response", "response")}} 、 {{domxref("XMLHttpRequest.responseText", "responseText")}} 和 {{domxref("XMLHttpRequest.responseXML", "responseXML")}}</li> + <li><a href="/zh-CN/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest">使用 XMLHttpRequest</a></li> + <li><a href="/zh-CN/docs/Web/API/XMLHttpRequest/HTML_in_XMLHttpRequest">HTML in XMLHttpRequest</a></li> + <li>{{DOMxRef("XMLHttpRequest.response", "response")}}, {{DOMxRef("XMLHttpRequest.responseText", "responseText")}} 和 {{DOMxRef("XMLHttpRequest.responseXML", "responseXML")}}</li> </ul> diff --git a/files/zh-cn/web/css/_doublecolon_before/index.html b/files/zh-cn/web/css/_doublecolon_before/index.html index e71e51a49b..c26c09f813 100644 --- a/files/zh-cn/web/css/_doublecolon_before/index.html +++ b/files/zh-cn/web/css/_doublecolon_before/index.html @@ -33,7 +33,7 @@ p::before { content: "Hello world!"; } </pre> <h3 id="Adding_quotation_marks" name="Adding_quotation_marks">加入引用标记</h3> -<p>使用 <code>::before </code>伪元素的一个简单示例就是用于加入引号。此处同时使用了 <code>::before</code> 和 <code>{{Cssref("::after")}}</code>来插入引用性文本。</p> +<p>使用 <code>::before </code>伪元素的一个简单示例就是用于加入引号。此处同时使用了 <code>::before</code> 和 <code>{{Cssxref("::after")}}</code>来插入引用性文本。</p> <h4 id="HTML_内容">HTML 内容</h4> diff --git a/files/zh-cn/web/css/css_background_and_borders/border-image_generator/index.html b/files/zh-cn/web/css/css_backgrounds_and_borders/border-image_generator/index.html index 85b576cfd7..870e84f663 100644 --- a/files/zh-cn/web/css/css_background_and_borders/border-image_generator/index.html +++ b/files/zh-cn/web/css/css_backgrounds_and_borders/border-image_generator/index.html @@ -1,10 +1,11 @@ --- title: Border-image 生成器 -slug: Web/CSS/CSS_Background_and_Borders/Border-image_generator +slug: Web/CSS/CSS_Backgrounds_and_Borders/Border-image_generator tags: - CSS - Tools translation_of: Web/CSS/CSS_Background_and_Borders/Border-image_generator +original_slug: Web/CSS/CSS_Background_and_Borders/Border-image_generator --- <p>这个工具用于生成 CSS3 {{cssxref("border-image")}} 的值.</p> diff --git a/files/zh-cn/web/css/css_background_and_borders/border-radius_generator/index.html b/files/zh-cn/web/css/css_backgrounds_and_borders/border-radius_generator/index.html index 70ffc1aad1..4781337bc3 100644 --- a/files/zh-cn/web/css/css_background_and_borders/border-radius_generator/index.html +++ b/files/zh-cn/web/css/css_backgrounds_and_borders/border-radius_generator/index.html @@ -1,8 +1,8 @@ --- title: 圆角边框生成器 -slug: Web/CSS/CSS_Background_and_Borders/Border-radius_generator +slug: Web/CSS/CSS_Backgrounds_and_Borders/Border-radius_generator translation_of: Web/CSS/CSS_Background_and_Borders/Border-radius_generator -original_slug: Web/CSS/CSS_Background_and_Borders/圆角边框发生器 +original_slug: Web/CSS/CSS_Background_and_Borders/Border-radius_generator --- <p>使用<em>Border-radius generator</em>生成 CSS3 {{cssxref("border-radius")}} 样式</p> diff --git a/files/zh-cn/web/css/css_background_and_borders/box-shadow_generator/index.html b/files/zh-cn/web/css/css_backgrounds_and_borders/box-shadow_generator/index.html index 79b9abe9e2..2847389264 100644 --- a/files/zh-cn/web/css/css_background_and_borders/box-shadow_generator/index.html +++ b/files/zh-cn/web/css/css_backgrounds_and_borders/box-shadow_generator/index.html @@ -1,8 +1,8 @@ --- title: Box-shadow generator -slug: Web/CSS/CSS_Background_and_Borders/Box-shadow_generator +slug: Web/CSS/CSS_Backgrounds_and_Borders/Box-shadow_generator translation_of: Web/CSS/CSS_Background_and_Borders/Box-shadow_generator -original_slug: Web/CSS/CSS_Box_Model/Box-shadow_generator +original_slug: Web/CSS/CSS_Background_and_Borders/Box-shadow_generator --- <p>这个可视化工具可以帮助你生成一个元素的CSS{{cssxref("box-shadow")}}相关代码,添加box shadow效果到你的CSS对象上。</p> diff --git a/files/zh-cn/web/css/css_flow_layout/index.html b/files/zh-cn/web/css/css_flow_layout/index.html index e6736b7023..4338eb2f88 100644 --- a/files/zh-cn/web/css/css_flow_layout/index.html +++ b/files/zh-cn/web/css/css_flow_layout/index.html @@ -37,7 +37,7 @@ translation_of: Web/CSS/CSS_Flow_Layout <li>{{Glossary("Block/CSS", "Block (CSS)")}}</li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/css_grid_layout/auto-placement_in_css_grid_layout/index.html b/files/zh-cn/web/css/css_grid_layout/auto-placement_in_css_grid_layout/index.html index 9386a5ef17..f1c3a4ecc2 100644 --- a/files/zh-cn/web/css/css_grid_layout/auto-placement_in_css_grid_layout/index.html +++ b/files/zh-cn/web/css/css_grid_layout/auto-placement_in_css_grid_layout/index.html @@ -555,7 +555,7 @@ dd { <p>可能你在使用自动定位或网格布局的其他功能时也会遇到问题,如果遇到了,请把它们抛出来,或者作为用例追加到已有问题上,一方面便于解决,另一方面也会帮助规范在未来的新版本变得更好。</p> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/zh-CN/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/zh-CN/docs/Web/CSS/Reference"><strong>CSS参考</strong></a></li> diff --git a/files/zh-cn/web/css/css_grid_layout/basic_concepts_of_grid_layout/index.html b/files/zh-cn/web/css/css_grid_layout/basic_concepts_of_grid_layout/index.html index 57d8f1b75c..0659a28b3a 100644 --- a/files/zh-cn/web/css/css_grid_layout/basic_concepts_of_grid_layout/index.html +++ b/files/zh-cn/web/css/css_grid_layout/basic_concepts_of_grid_layout/index.html @@ -685,7 +685,7 @@ translation_of: Web/CSS/CSS_Grid_Layout/Basic_Concepts_of_Grid_Layout <p>在本文章中我们快速对网格布局的概念有了个基本的了解。利用示例代码进行练习,然后<a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Grid_Layout/Relationship_of_Grid_Layout">接下来我们正式开始挖掘关于网格布局的各种细节</a>。</p> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><strong><a href="/zh-CN/docs/Web/CSS/Reference">CSS 参考</a></strong></li> diff --git a/files/zh-cn/web/css/css_grid_layout/box_alignment_in_css_grid_layout/index.html b/files/zh-cn/web/css/css_grid_layout/box_alignment_in_css_grid_layout/index.html index 0b5c506a1f..9496e10a87 100644 --- a/files/zh-cn/web/css/css_grid_layout/box_alignment_in_css_grid_layout/index.html +++ b/files/zh-cn/web/css/css_grid_layout/box_alignment_in_css_grid_layout/index.html @@ -643,7 +643,7 @@ translation_of: Web/CSS/CSS_Grid_Layout/Box_Alignment_in_CSS_Grid_Layout <p>CSS 网格布局和盒模型对齐规范是为了解决 CSS 中的书写模式而设计的,这意味着,如果你用一种从右到左的语言工作,比如阿拉伯语,那网格的起始线就应该是从右上开始,此时 justify-content: start 也将使网格轨道从网格的右侧开始。为 <code>margin-right</code> 和 <code>margin-left</code> 设置自动外边距,或者使用绝对位置如 <code>top<font face="Open Sans, arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">,</span></font></code><code>right<font face="Open Sans, arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">,</span></font></code><code>bottom</code> 和 <code>left</code> 来设置偏移量对书写模式来说并不是一种友好的表达方式。在下一篇文章中,我们会深入讨论 CSS 网格布局、盒模型对齐和书写模式之间的关系,理解这些原理,对于开发用多种语言显示的网站,或者设计混合了多种语言和书写模式的页面将大有益处。</p> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/zh-CN/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/zh-CN/docs/Web/CSS/Reference"><strong>CSS参考</strong></a></li> diff --git a/files/zh-cn/web/css/css_grid_layout/css_grid_and_progressive_enhancement/index.html b/files/zh-cn/web/css/css_grid_layout/css_grid_and_progressive_enhancement/index.html index e67b69a9fa..3e606beaa6 100644 --- a/files/zh-cn/web/css/css_grid_layout/css_grid_and_progressive_enhancement/index.html +++ b/files/zh-cn/web/css/css_grid_layout/css_grid_and_progressive_enhancement/index.html @@ -393,7 +393,7 @@ img { <li><a href="https://css-tricks.com/css-grid-in-ie-css-grid-and-the-new-autoprefixer">CSS Grid and the New Autoprefixer</a></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/css_grid_layout/css_grid_layout_and_accessibility/index.html b/files/zh-cn/web/css/css_grid_layout/css_grid_layout_and_accessibility/index.html index 286a249901..a68be27726 100644 --- a/files/zh-cn/web/css/css_grid_layout/css_grid_layout_and_accessibility/index.html +++ b/files/zh-cn/web/css/css_grid_layout/css_grid_layout_and_accessibility/index.html @@ -121,7 +121,7 @@ translation_of: Web/CSS/CSS_Grid_Layout/CSS_Grid_Layout_and_Accessibility <p>作为一种开始思考这些问题的方式,当使用 CSS 网格布局时,建议阅读 Leonie Watson 的 <em><a href="http://tink.uk/flexbox-the-keyboard-navigation-disconnect/">Flexbox & the Keyboard Navigation Disconnect</a>。<a href="https://www.youtube.com/watch?v=spxT2CmHoPk">来自 ffconf 的 Leoie 的演示视频</a>有助于了解屏幕阅读器如何使用 CSS 的视觉表示进行工作。Adrian Roselli 也发表了 <a href="http://adrianroselli.com/2015/10/html-source-order-vs-css-display-order.html">tab order in various browsers</a> —— 尽管这是在 Firefox 完全实现网格支持之前。</em></p> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><em><a href="/zh-CN/docs/Web/CSS"><strong>CSS</strong></a></em></li> <li><em><a href="/zh-CN/docs/Web/CSS/Reference"><strong>CSS参考</strong></a></em></li> diff --git a/files/zh-cn/web/css/css_grid_layout/css_grid_logical_values_and_writing_modes/index.html b/files/zh-cn/web/css/css_grid_layout/css_grid_logical_values_and_writing_modes/index.html index 216a6fa669..9608cd5fda 100644 --- a/files/zh-cn/web/css/css_grid_layout/css_grid_logical_values_and_writing_modes/index.html +++ b/files/zh-cn/web/css/css_grid_layout/css_grid_logical_values_and_writing_modes/index.html @@ -444,7 +444,7 @@ original_slug: Web/CSS/CSS_Grid_Layout/CSS_Grid,_Logical_Values_and_Writing_Mode <p><a href="https://drafts.csswg.org/css-logical-props/">CSS 逻辑属性规范</a> 的目标是改变现状,在未来的 CSS 中,{{cssxref("margin-left")}} 和 {{cssxref("margin-right")}} 将与<a href="/en-US/docs/Web/CSS/CSS_Logical_Properties">逻辑属性</a>相同。Firefox 已经实现了,所以你现在就可以在 Firefox 中试上一试。未来如果得到全面支持,那么通过网格学习到的“块和行内”的知识,你也能马上举一反三地用在其他地方了。</p> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/zh-CN/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/zh-CN/docs/Web/CSS/Reference"><strong>CSS参考</strong></a></li> diff --git a/files/zh-cn/web/css/css_grid_layout/grid_template_areas/index.html b/files/zh-cn/web/css/css_grid_layout/grid_template_areas/index.html index bdbddab744..5c3d8b05d5 100644 --- a/files/zh-cn/web/css/css_grid_layout/grid_template_areas/index.html +++ b/files/zh-cn/web/css/css_grid_layout/grid_template_areas/index.html @@ -482,7 +482,7 @@ translation_of: Web/CSS/CSS_Grid_Layout/Grid_Template_Areas <p>如果你通过阅读本指南前面的几篇文章已经开始上手了,那么你应该是用基于线的定位方式或用命名区域的方式来创建网格布局的。花费一些时间用网格去创建常见的布局模式,虽然还有很多的术语要学习,不过语法是非常一目了然的。在你练习开发的过程中可能会遭遇到一些问题,并且用我们没有讲过的方法去解决它们。在本指南后面的文章中,我们将着眼于更多规范中的细节 —— 目标是让你能用它创建出更加复杂的布局。</p> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/zh-CN/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/zh-CN/docs/Web/CSS/Reference"><strong>CSS参考</strong></a></li> diff --git a/files/zh-cn/web/css/css_grid_layout/index.html b/files/zh-cn/web/css/css_grid_layout/index.html index 17f637d96f..46faef1a06 100644 --- a/files/zh-cn/web/css/css_grid_layout/index.html +++ b/files/zh-cn/web/css/css_grid_layout/index.html @@ -204,7 +204,7 @@ translation_of: Web/CSS/CSS_Grid_Layout </tbody> </table> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/zh-CN/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/zh-CN/docs/Web/CSS/Reference"><strong>CSS索引</strong></a></li> diff --git a/files/zh-cn/web/css/css_grid_layout/layout_using_named_grid_lines/index.html b/files/zh-cn/web/css/css_grid_layout/layout_using_named_grid_lines/index.html index 86699a10f4..4ca790f7b7 100644 --- a/files/zh-cn/web/css/css_grid_layout/layout_using_named_grid_lines/index.html +++ b/files/zh-cn/web/css/css_grid_layout/layout_using_named_grid_lines/index.html @@ -428,7 +428,7 @@ translation_of: Web/CSS/CSS_Grid_Layout/Layout_using_Named_Grid_Lines <p>这就是我们需要的,不用做任何计算,网格在分配 1fr 的列轨道空间之前会自动把 10 像素的轨道间距删除掉。在你动手创建自己的布局的过程中,语法会变得越来越熟悉,你能找到适于自己的方法和喜欢创建的项目类型。尝试用不同的方法去创建一些通用的布局模式,你会找到最适合自己的工作方式。在下一篇文章中我们会探讨如何用网格定位元素 —— 根本不需要使用定位属性!</p> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/zh-CN/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/zh-CN/docs/Web/CSS/Reference"><strong>CSS参考</strong></a></li> diff --git a/files/zh-cn/web/css/css_grid_layout/line-based_placement_with_css_grid/index.html b/files/zh-cn/web/css/css_grid_layout/line-based_placement_with_css_grid/index.html index 004634d8b8..89a3d38534 100644 --- a/files/zh-cn/web/css/css_grid_layout/line-based_placement_with_css_grid/index.html +++ b/files/zh-cn/web/css/css_grid_layout/line-based_placement_with_css_grid/index.html @@ -600,7 +600,7 @@ translation_of: Web/CSS/CSS_Grid_Layout/Line-based_Placement_with_CSS_Grid <p>再者,记住你明确定位的元素是可以互相重叠的。有时你能用这点创造一些有趣的效果,但有时也会因为你定位错了线而导致不正确的重叠。<a href="/en-US/docs/Tools/Page_Inspector/How_to/Examine_grid_layouts">Firefox Grid Highlighter</a> 可以在你学习时给你帮助,特别是当你的网格比较复杂时。</p> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/zh-CN/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/zh-CN/docs/Web/CSS/Reference"><strong>CSS索引</strong></a></li> diff --git a/files/zh-cn/web/css/css_grid_layout/realizing_common_layouts_using_css_grid_layout/index.html b/files/zh-cn/web/css/css_grid_layout/realizing_common_layouts_using_css_grid_layout/index.html index a20d8b626a..e052b14da2 100644 --- a/files/zh-cn/web/css/css_grid_layout/realizing_common_layouts_using_css_grid_layout/index.html +++ b/files/zh-cn/web/css/css_grid_layout/realizing_common_layouts_using_css_grid_layout/index.html @@ -535,7 +535,7 @@ original_slug: Web/CSS/CSS_Grid_Layout/利用CSS网格布局实现常用布局 <li>有关其他常见布局模式,请参见 <a href="http://gridbyexample.com">Grid by Example</a>, 这里有许多网格布局的小例子,也有一些较大的UI模式和完整的页面布局。</li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/css_grid_layout/relationship_of_grid_layout/index.html b/files/zh-cn/web/css/css_grid_layout/relationship_of_grid_layout/index.html index 6294c4aa25..2f3ed187ba 100644 --- a/files/zh-cn/web/css/css_grid_layout/relationship_of_grid_layout/index.html +++ b/files/zh-cn/web/css/css_grid_layout/relationship_of_grid_layout/index.html @@ -580,7 +580,7 @@ translation_of: Web/CSS/CSS_Grid_Layout/Relationship_of_Grid_Layout <p>正如本文中所述,CSS Grid Layout 只是您工具包的一部分。不要害怕在布局时将它与其他布局方法混用,大胆用它获得你想要的效果吧。</p> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/zh-CN/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/zh-CN/docs/Web/CSS/Reference"><strong>CSS索引</strong></a></li> diff --git a/files/zh-cn/web/css/grid-area/index.html b/files/zh-cn/web/css/grid-area/index.html index d5c5c3d9e2..4f35049aee 100644 --- a/files/zh-cn/web/css/grid-area/index.html +++ b/files/zh-cn/web/css/grid-area/index.html @@ -220,7 +220,7 @@ grid-area: unset;</pre> <li>Video tutorial: <em><a href="http://gridbyexample.com/video/grid-template-areas/">Grid Template Areas</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/grid-auto-columns/index.html b/files/zh-cn/web/css/grid-auto-columns/index.html index 7b24f9199e..fa64012b44 100644 --- a/files/zh-cn/web/css/grid-auto-columns/index.html +++ b/files/zh-cn/web/css/grid-auto-columns/index.html @@ -149,7 +149,7 @@ grid-auto-columns: unset; <li>Video tutorial: <em><a href="http://gridbyexample.com/video/series-auto-placement-order/">Introducing Grid auto-placement and order</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/grid-auto-flow/index.html b/files/zh-cn/web/css/grid-auto-flow/index.html index bf53cdd92b..3f6d6cf6c0 100644 --- a/files/zh-cn/web/css/grid-auto-flow/index.html +++ b/files/zh-cn/web/css/grid-auto-flow/index.html @@ -154,7 +154,7 @@ grid-auto-flow: unset;</pre> <li>Video tutorial: <em><a href="http://gridbyexample.com/video/series-auto-placement-order/">Introducing Grid auto-placement and order</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/grid-auto-rows/index.html b/files/zh-cn/web/css/grid-auto-rows/index.html index a448a6833b..536e625a8a 100644 --- a/files/zh-cn/web/css/grid-auto-rows/index.html +++ b/files/zh-cn/web/css/grid-auto-rows/index.html @@ -148,7 +148,7 @@ grid-auto-rows: unset; <li>视频教程:<em><a href="http://gridbyexample.com/video/series-auto-placement-order/">引入网格自动放置和排序</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/zh-CN/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/zh-CN/docs/Web/CSS/Reference"><strong>CSS参考</strong></a></li> diff --git a/files/zh-cn/web/css/grid-column/index.html b/files/zh-cn/web/css/grid-column/index.html index d826b70c60..c8f01a098a 100644 --- a/files/zh-cn/web/css/grid-column/index.html +++ b/files/zh-cn/web/css/grid-column/index.html @@ -138,7 +138,7 @@ translation_of: Web/CSS/grid-column <li>Video tutorial: <em><a href="http://gridbyexample.com/video/series-line-based-placement/">Line-based placement</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/grid-row/index.html b/files/zh-cn/web/css/grid-row/index.html index 4f5826271a..83baa1020b 100644 --- a/files/zh-cn/web/css/grid-row/index.html +++ b/files/zh-cn/web/css/grid-row/index.html @@ -144,7 +144,7 @@ grid-row: unset; <li>Video tutorial: <em><a href="http://gridbyexample.com/video/series-line-based-placement/">Line-based placement</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/grid-template-areas/index.html b/files/zh-cn/web/css/grid-template-areas/index.html index 15cdc56df2..89b09a064f 100644 --- a/files/zh-cn/web/css/grid-template-areas/index.html +++ b/files/zh-cn/web/css/grid-template-areas/index.html @@ -141,7 +141,7 @@ grid-template-areas: unset; /* 未设置 */ <li>视频教程<em>:<a href="http://gridbyexample.com/video/grid-template-areas/">Grid Template Areas</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/grid-template-columns/index.html b/files/zh-cn/web/css/grid-template-columns/index.html index bb3150a257..3cd25ea361 100644 --- a/files/zh-cn/web/css/grid-template-columns/index.html +++ b/files/zh-cn/web/css/grid-template-columns/index.html @@ -140,7 +140,7 @@ grid-template-columns: unset; <li>视频教程: <em><a href="http://gridbyexample.com/video/series-define-a-grid/">Defining a Grid</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/grid-template-rows/index.html b/files/zh-cn/web/css/grid-template-rows/index.html index fdffd201ff..7ab2365eeb 100644 --- a/files/zh-cn/web/css/grid-template-rows/index.html +++ b/files/zh-cn/web/css/grid-template-rows/index.html @@ -153,7 +153,7 @@ grid-template-rows: unset; <li>视频教程: <em><a href="http://gridbyexample.com/video/series-define-a-grid/">Defining a Grid</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/grid-template/index.html b/files/zh-cn/web/css/grid-template/index.html index 0b79fc80d3..7b305e2862 100644 --- a/files/zh-cn/web/css/grid-template/index.html +++ b/files/zh-cn/web/css/grid-template/index.html @@ -146,7 +146,7 @@ footer { <li>视频教程:<em> <a href="http://gridbyexample.com/video/grid-template-shorthand/">Grid Template shorthand</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/grid/index.html b/files/zh-cn/web/css/grid/index.html index 2e4e255ebe..ff04b9027c 100644 --- a/files/zh-cn/web/css/grid/index.html +++ b/files/zh-cn/web/css/grid/index.html @@ -133,7 +133,7 @@ grid: unset; <li><em>网格布局指南:</em><em><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Grid_Template_Areas#Grid_definition_shorthands">Grid template areas - Grid definition shorthands</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li><a href="/en-US/docs/Web/CSS"><strong>CSS</strong></a></li> <li><a href="/en-US/docs/Web/CSS/Reference"><strong>CSS Reference</strong></a></li> diff --git a/files/zh-cn/web/css/minmax()/index.html b/files/zh-cn/web/css/minmax()/index.html index 3d78c94ddf..4509e35f5d 100644 --- a/files/zh-cn/web/css/minmax()/index.html +++ b/files/zh-cn/web/css/minmax()/index.html @@ -138,7 +138,7 @@ minmax(auto, 300px) <li>视频教程:<em><a href="http://gridbyexample.com/video/series-minmax/">Introducing minmax()</a></em></li> </ul> -<section class="Quick_links" id="Quick_Links"> +<section id="Quick_links"> <ol> <li> </li> <li><a href="/zh-CN/docs/Web/CSS"><strong>CSS</strong></a></li> diff --git a/files/zh-cn/web/css/tools/index.html b/files/zh-cn/web/css/tools/index.html deleted file mode 100644 index a4c348fb36..0000000000 --- a/files/zh-cn/web/css/tools/index.html +++ /dev/null @@ -1,27 +0,0 @@ ---- -title: 工具 -slug: Web/CSS/Tools -tags: - - CSS -translation_of: Web/CSS/Tools ---- -<div>CSS提供了许多强大的功能,但这些功能要么可能很难使用,要么有许多参数,因此在使用它们时,能够可视化它们是有帮助的。此页面提供了一些有用的工具的链接,这些工具将帮助您使用这些功能构建 CSS 来设计您的内容。</div> - -<p> </p> - -<p>{{LandingPageListSubpages}}</p> - -<h2 id="其他工具">其他工具</h2> - -<ul> - <li>CSS 动画- <a href="http://jeremyckahn.github.io/stylie/">Stylie</a></li> - <li>查看设备的显示信息(从 {{Glossary("响应式网页设计")}}处得到帮助) - <a href="http://mydevice.io">mydevice.io</a></li> - <li>CSS 菜单- <a href="http://cssmenumaker.com/">cssmenumaker.com</a></li> - <li>一个 强大并现代化的 CSS linter 可以帮助你实现统一约定并避免样式表中的错误 - <a href="https://stylelint.io/">stylelint</a></li> -</ul> - -<h2 id="相关主题">相关主题</h2> - -<ul> - <li><a href="/zh-CN/docs/Web/CSS" title="/en-US/docs/Web/CSS">CSS</a></li> -</ul> diff --git a/files/zh-cn/web/demos_of_open_web_technologies/index.html b/files/zh-cn/web/demos/index.html index 29978d4ed2..04c11c2257 100644 --- a/files/zh-cn/web/demos_of_open_web_technologies/index.html +++ b/files/zh-cn/web/demos/index.html @@ -1,6 +1,6 @@ --- title: 开源 Web 技术示例 -slug: Web/Demos_of_open_web_technologies +slug: Web/Demos tags: - 2D - 3D @@ -11,7 +11,7 @@ tags: - SVG - Video translation_of: Web/Demos_of_open_web_technologies -original_slug: Web/演示说明 +original_slug: Web/Demos_of_open_web_technologies --- <p class="summary">Mozilla 支持各种令人兴奋的开源 Web 技术,我们鼓励大家使用它们。此页面提供了有关这些技术的一些有趣演示链接。</p> diff --git a/files/zh-cn/web/houdini/index.html b/files/zh-cn/web/guide/houdini/index.html index 68259ebd34..2c81ff1ed3 100644 --- a/files/zh-cn/web/houdini/index.html +++ b/files/zh-cn/web/guide/houdini/index.html @@ -1,12 +1,13 @@ --- title: CSS Houdini -slug: Web/Houdini +slug: Web/Guide/Houdini tags: - API - CSS - Houdini - Web translation_of: Web/Houdini +original_slug: Web/Houdini --- <p>Houdini是一组底层API,它们公开了CSS引擎的各个部分,从而使开发人员能够通过加入浏览器渲染引擎的样式和布局过程来扩展CSS。 Houdini是一组API,它们使开发人员可以直接访问<a href="/en-US/docs/Web/API/CSS_Object_Model">CSS 对象模型</a> (CSSOM),使开发人员可以编写浏览器可以解析为CSS的代码,从而创建新的CSS功能,而无需等待它们在浏览器中本地实现。</p> diff --git a/files/zh-cn/web/http/methods/index.html b/files/zh-cn/web/http/methods/index.html index 6f926a633d..036f553cf5 100644 --- a/files/zh-cn/web/http/methods/index.html +++ b/files/zh-cn/web/http/methods/index.html @@ -14,15 +14,15 @@ translation_of: Web/HTTP/Methods --- <p>{{HTTPSidebar}}</p> -<p>HTTP 定义了一组<strong>请求方法</strong>, 以表明要对给定资源执行的操作。指示针对给定资源要执行的期望动作. 虽然他们也可以是名词, 但这些请求方法有时被称为HTTP动词. 每一个请求方法都实现了不同的语义, 但一些共同的特征由一组共享:: 例如一个请求方法可以是 {{glossary("safe")}}, {{glossary("idempotent")}}, 或 {{glossary("cacheable")}}.</p> +<p>HTTP 定义了一组<strong>请求方法</strong>,以表明要对给定资源执行的操作。指示针对给定资源要执行的期望动作。虽然他们也可以是名词, 但这些请求方法有时被称为HTTP动词。每一个请求方法都实现了不同的语义,但一些共同的特征由一组共享:例如一个请求方法可以是 {{glossary("safe")}}, {{glossary("idempotent")}}, 或 {{glossary("cacheable")}}。</p> <dl> <dt><code><a href="/en-US/docs/Web/HTTP/Methods/GET">GET</a></code></dt> - <dd>GET方法请求一个指定资源的表示形式. 使用GET的请求应该只被用于获取数据.</dd> + <dd>GET方法请求一个指定资源的表示形式,使用GET的请求应该只被用于获取数据。</dd> <dt><code><a href="/en-US/docs/Web/HTTP/Methods/HEAD">HEAD</a></code></dt> - <dd>HEAD方法请求一个与GET请求的响应相同的响应,但没有响应体.</dd> + <dd>HEAD方法请求一个与GET请求的响应相同的响应,但没有响应体。</dd> <dt><code><a href="/en-US/docs/Web/HTTP/Methods/POST">POST</a></code></dt> - <dd>POST方法用于将实体提交到指定的资源,通常导致在服务器上的状态变化或副作用. </dd> + <dd>POST方法用于将实体提交到指定的资源,通常导致在服务器上的状态变化或副作用。</dd> <dt><code><a href="/en-US/docs/Web/HTTP/Methods/PUT">PUT</a></code></dt> <dd>PUT方法用请求有效载荷替换目标资源的所有当前表示。</dd> <dt><code><a href="/en-US/docs/Web/HTTP/Methods/DELETE">DELETE</a></code></dt> diff --git a/files/zh-cn/web/javascript/guide/grammar_and_types/index.html b/files/zh-cn/web/javascript/guide/grammar_and_types/index.html index 0ea7538a96..8b00401fcc 100644 --- a/files/zh-cn/web/javascript/guide/grammar_and_types/index.html +++ b/files/zh-cn/web/javascript/guide/grammar_and_types/index.html @@ -447,8 +447,8 @@ console.log(a[0]); // 3</pre> <pre class="brush: js notranslate">3.14 -.2345789 // -0.23456789 --3.12e+12 // -3.12*10<sup>12</sup> -.1e-23 // 0.1*10<sup>-23</sup>=10<sup>-24</sup>=1e-24 +-3.12e+12 // -3.12*10^12 +.1e-23 // 0.1*10^(-23)=10^(-24)=1e-24 </pre> <h3 id="对象字面量_Object_literals">对象字面量 (Object literals)</h3> diff --git a/files/zh-cn/web/javascript/guide/text_formatting/index.html b/files/zh-cn/web/javascript/guide/text_formatting/index.html index 522b3ed362..eac38b4502 100644 --- a/files/zh-cn/web/javascript/guide/text_formatting/index.html +++ b/files/zh-cn/web/javascript/guide/text_formatting/index.html @@ -62,7 +62,7 @@ typeof foo; // 返回 'object'</pre> <pre class="brush: js notranslate">const firstString = '2 + 2'; //创建一个字符串字面量 const secondString = new String('2 + 2'); // 创建一个字符串对象 eval(firstString); // 返回数字 4 -eval(secondString); // 返回字符串 "2 + 2"</pre> +eval(secondString); // 返回包含 "2 + 2" 的字符串对象</pre> <p><code>String</code> 对象有一个属性 <code>length</code>,标识了字符串中 UTF-16 的码点个数。举例,下面的代码把 13 赋值给了<code>helloLength</code>,因为 "Hello, World!" 包含 13 个字符,每个字符用一个 UTF-16 码点表示。你可以通过数组的方式访问每一个码点,但你不能修改每个字符,因为字符串是不变的类数组对象: </p> diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/array/index.md b/files/zh-cn/web/javascript/reference/global_objects/array/array/index.md new file mode 100644 index 0000000000..708d629c23 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/array/array/index.md @@ -0,0 +1,81 @@ +--- +title: Array() 构造器 +slug: Web/JavaScript/Reference/Global_Objects/Array/Array +tags: + - 数组 + - 构造器 + - JavaScript + - 参考手册 +browser-compat: javascript.builtins.Array.Array +translation_of: Web/JavaScript/Reference/Global_Objects/Array/Array +--- +{{JSRef}} + +**`Array()`** 构造器用于创建 {{jsxref("Array")}} 对象。 + +## 语法 + +```js +// literal constructor +[element0, element1, ..., elementN] + +// construct from elements +new Array(element0, element1, ..., elementN) + +// construct from array length +new Array(arrayLength) +``` + +### 参数 + +- `elementN` + - : `Array` 构造器会根据给定的元素创建一个 JavaScript 数组,但是当仅有一个参数且为数字时除外(详见下面的 `arrayLength` 参数)。注意,后者仅适用于用 Array 构造器创建数组,而不适用于用方括号创建的数组字面量。 +- `arrayLength` + - : 一个范围在 0 到 2^32 - 1 之间的整数,此时将返回一个 `length` 的值等于 `arrayLength` 的数组对象(言外之意就是该数组此时并没有包含任何实际的元素,不能理所当然地认为它包含 `arrayLength` 个值为 undefined 的元素)。如果传入的参数不是有效值,则会抛出 {{jsxref("RangeError")}} 异常 + +## 示例 + +### 数组字面量 + +可以通过使用 [数组字面量](/zn-CN/docs/Web/JavaScript/Reference/Lexical_grammar#Array_literals) 创建数组 + +```js +let fruits = ['Apple', 'Banana']; + +console.log(fruits.length); // 2 +console.log(fruits[0]); // "Apple" +``` + +### 单个参数的 Array 构造器 + +可以通过单个数字参数的构造器创建数组,数组的长度为传入的参数,该数组不包含任何实际的元素 + +```js +let fruits = new Array(2); + +console.log(fruits.length); // 2 +console.log(fruits[0]); // undefined +``` + +### 多个参数的 Array 构造器 + +如果向构造器传入多个参数,则会创建一个包含所有传入参数的新数组 + +```js +let fruits = new Array('Apple', 'Banana'); + +console.log(fruits.length); // 2 +console.log(fruits[0]); // "Apple" +``` + +## 规范 + +{{Specifications}} + +## 浏览器兼容性 + +{{Compat}} + +## 相关链接 + +- {{jsxref("Array")}} class diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/index.html deleted file mode 100644 index 3b16a483c8..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/array/index.html +++ /dev/null @@ -1,443 +0,0 @@ ---- -title: Array -slug: Web/JavaScript/Reference/Global_Objects/Array -tags: - - JavaScript - - 二维数组 - - 全局对象 - - 参考手册 - - 数组 -translation_of: Web/JavaScript/Reference/Global_Objects/Array ---- -<div>{{JSRef}}</div> - -<div></div> - -<p>JavaScript的 <code><strong>Array</strong></code> 对象是用于构造数组的全局对象,数组是类似于列表的高阶对象。</p> - -<p><strong>创建数组</strong></p> - -<pre class="brush: js">var fruits = ['Apple', 'Banana']; - -console.log(fruits.length); -// 2 -</pre> - -<p><strong>通过索引访问数组元素</strong></p> - -<pre class="brush: js">var first = fruits[0]; -// Apple - -var last = fruits[fruits.length - 1]; -// Banana</pre> - -<p><strong>遍历数组</strong></p> - -<pre class="brush: js">fruits.forEach(function (item, index, array) { - console.log(item, index); -}); -// Apple 0 -// Banana 1</pre> - -<p><strong>添加元素到数组的末尾</strong></p> - -<pre class="brush: js">var newLength = fruits.push('Orange'); -// newLength:3; fruits: ["Apple", "Banana", "Orange"]</pre> - -<p><strong>删除数组末尾的元素</strong></p> - -<pre class="brush: js">var last = fruits.pop(); // remove Orange (from the end) -// last: "Orange"; fruits: ["Apple", "Banana"];</pre> - -<p><strong>删除数组最前面(头部)的元素</strong></p> - -<pre class="brush: js">var first = fruits.shift(); // remove Apple from the front -// first: "Apple"; fruits: ["Banana"];</pre> - -<p><strong>添加元素到数组的头部</strong></p> - -<pre class="brush: js">var newLength = fruits.unshift('Strawberry') // add to the front -// ["Strawberry", "Banana"]; -</pre> - -<p><strong>找出某个元素在数组中的索引</strong></p> - -<pre class="brush: js">fruits.push('Mango'); -// ["Strawberry", "Banana", "Mango"] - -var pos = fruits.indexOf('Banana'); -// 1</pre> - -<p><strong>通过索引删除某个元素</strong></p> - -<pre class="brush: js">var removedItem = fruits.splice(pos, 1); // this is how to remove an item - -// ["Strawberry", "Mango"]</pre> - -<p><strong>从一个索引位置删除多个元素</strong></p> - -<pre class="brush: js">var vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot']; -console.log(vegetables); -// ["Cabbage", "Turnip", "Radish", "Carrot"] - -var pos = 1, n = 2; - -var removedItems = vegetables.splice(pos, n); -// this is how to remove items, n defines the number of items to be removed, -// from that position(pos) onward to the end of array. - -console.log(vegetables); -// ["Cabbage", "Carrot"] (the original array is changed) - -console.log(removedItems); -// ["Turnip", "Radish"]</pre> - -<p><strong>复制一个数组</strong></p> - -<pre class="brush: js">var shallowCopy = fruits.slice(); // this is how to make a copy -// ["Strawberry", "Mango"]</pre> - -<h2 id="语法">语法</h2> - -<pre class="syntaxbox">[<var>element0</var>, <var>element1</var>, ..., <var>elementN</var>] -new Array(<var>element0</var>, <var>element1</var>[, ...[, <var>elementN</var>]]) -new Array(<var>arrayLength</var>) -</pre> - -<dl> - <dt> - <h3 id="参数">参数</h3> - </dt> - <dt><code>elementN</code></dt> - <dd><code>Array</code> 构造器会根据给定的元素创建一个 JavaScript 数组,但是当仅有一个参数且为数字时除外(详见下面的 <code>arrayLength</code> 参数)。注意,后面这种情况仅适用于用 <code>Array</code> 构造器创建数组,而不适用于用方括号创建的数组字面量。</dd> - <dt><code>arrayLength</code></dt> - <dd>一个范围在 0 到 2<sup>32</sup>-1 之间的整数,此时将返回一个 <code>length</code> 的值等于 <code>arrayLength</code> 的数组对象(言外之意就是该数组此时并没有包含任何实际的元素,不能理所当然地认为它包含 <code>arrayLength</code> 个值为 <code>undefined</code> 的元素)。如果传入的参数不是有效值,则会抛出 {{jsxref("RangeError")}} 异常。</dd> -</dl> - -<h2 id="描述">描述</h2> - -<p>数组是一种类列表对象,它的原型中提供了遍历和修改元素的相关操作。JavaScript 数组的长度和元素类型都是非固定的。因为数组的长度可随时改变,并且其数据在内存中也可以不连续,所以 JavaScript 数组不一定是密集型的,这取决于它的使用方式。一般来说,数组的这些特性会给使用带来方便,但如果这些特性不适用于你的特定使用场景的话,可以考虑使用类型数组 {{jsxref("TypedArray")}}。</p> - -<p>只能用整数作为数组元素的索引,而不能用字符串。后者称为<a href="https://en.wikipedia.org/wiki/Associative_array">关联数组</a>。使用非整数并通过<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties">方括号</a>或<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Property_Accessors">点号</a>来访问或设置数组元素时,所操作的并不是数组列表中的元素,而是数组对象的<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures#Properties">属性集合</a>上的变量。数组对象的属性和数组元素列表是分开存储的,并且数组的遍历和修改操作也不能作用于这些命名属性。</p> - -<h3 id="访问数组元素">访问数组元素</h3> - -<p>JavaScript 数组的索引是从0开始的,第一个元素的索引为0,最后一个元素的索引等于该数组的长度减1。如果指定的索引是一个无效值,JavaScript 数组并不会报错,而是会返回 <code>undefined</code>。</p> - -<pre class="brush: js language-js">var arr = ['this is the first element', 'this is the second element', 'this is the last element']; -console.log(arr[0]); // 打印 'this is the first element' -console.log(arr[1]); // 打印 'this is the second element' -console.log(arr[arr.length - 1]); // 打印 'this is the last element' -</pre> - -<p>虽然数组元素可以看做是数组对象的属性,就像 <code>toString</code> 一样,但是下面的写法是错误的,运行时会抛出 <code>SyntaxError</code> 异常,而原因则是使用了非法的属性名:</p> - -<pre class="brush: js language-js">console.log(arr.0); // a syntax error -</pre> -<p>并不是 JavaScript 数组有什么特殊之处,而是因为在 JavaScript 中,以数字开头的属性不能用点号引用,必须用方括号。比如,如果一个对象有一个名为 <code>3d</code> 的属性,那么只能用方括号来引用它。下面是具体的例子:</p> - -<pre class="brush: js language-js">var years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]; -console.log(years.0); // 语法错误 -console.log(years[0]); // √ -</pre> -<pre class="brush: js language-js">renderer.3d.setTexture(model, 'character.png'); // 语法错误 -renderer['3d'].setTexture(model, 'character.png'); // √ -</pre> - -<p>注意在 <code>3d</code> 那个例子中,引号是必须的。你也可以将数组的索引用引号引起来,比如 <code>years[2]</code> 可以写成 <code>years['2']</code>。 <code>years[2]</code> 中的 2 会被 JavaScript 解释器通过调用 <code>toString</code> 隐式转换成字符串。正因为这样,<code>'2'</code> 和 <code>'02'</code> 在 <code>years</code> 中所引用的可能是不同位置上的元素。而下面这个例子也可能会打印 <code>true</code>:</p> - -<pre class="brush: js language-js">console.log(years['2'] != years['02']); -</pre> -<p>类似地,如果对象的属性名称是保留字(最好不要这么做!),那么就只能通过字符串的形式用方括号来访问(从 firefox 40.0a2 开始也支持用点号访问了):</p> - -<pre class="brush: js language-js">var promise = { - 'var' : 'text', - 'array': [1, 2, 3, 4] -}; - -console.log(promise['var']); -</pre> - -<h3 id="length_和数字下标之间的关系">length 和数字下标之间的关系</h3> - -<p>JavaScript 数组的 {{jsxref("Array.length", "length")}} 属性和其数字下标之间有着紧密的联系。数组内置的几个方法(例如 {{jsxref("Array.join", "join")}}、{{jsxref("Array.slice", "slice")}}、{{jsxref("Array.indexOf", "indexOf")}} 等)都会考虑 {{jsxref("Array.length", "length")}} 的值。另外还有一些方法(例如 {{jsxref("Array.push", "push")}}、{{jsxref("Array.splice", "splice")}} 等)还会改变 {{jsxref("Array.length", "length")}} 的值。</p> - -<pre class="brush: js language-js">var fruits = []; -fruits.push('banana', 'apple', 'peach'); - -console.log(fruits.length); // 3 -</pre> - -<p>使用一个合法的下标为数组元素赋值,并且该下标超出了当前数组的大小的时候,解释器会同时修改 {{jsxref("Array.length", "length")}} 的值:</p> - -<pre class="brush: js language-js">fruits[5] = 'mango'; -console.log(fruits[5]); // 'mango' -console.log(Object.keys(fruits)); // ['0', '1', '2', '5'] -console.log(fruits.length); // 6 -</pre> - -<p>也可以显式地给 {{jsxref("Array.length", "length")}} 赋一个更大的值:</p> - -<pre class="brush: js language-js">fruits.length = 10; -console.log(Object.keys(fruits)); // ['0', '1', '2', '5'] -console.log(fruits.length); // 10 -</pre> -<p>而为 {{jsxref("Array.length", "length")}} 赋一个更小的值则会删掉一部分元素:</p> - -<pre class="brush: js language-js">fruits.length = 2; -console.log(Object.keys(fruits)); // ['0', '1'] -console.log(fruits.length); // 2</pre> - -<p>这一节的内容在 {{jsxref("Array.length")}} 中有更详细的介绍。</p> -<h3 id="正则匹配结果所返回的数组">正则匹配结果所返回的数组</h3> - -<p>使用正则表达式匹配字符串可以得到一个数组。这个数组中包含本次匹配的相关信息和匹配结果。{{jsxref("RegExp.exec")}}、{{jsxref("String.match")}}、{{jsxref("String.replace")}} 都会返回这样的数组。看下面的例子和例子下面的表格:</p> - -<pre class="brush: js">// 匹配1个 d 后面紧跟着至少1个 b,再后面又跟着1个 d 的子串, -// 并且需要记住子串中匹配到的 b 和最后的 d (通过正则表达式中的分组), -// 同时在匹配时忽略大小写 - -myRe = /d(b+)(d)/i; -myArray = myRe.exec("cdbBdbsbz"); -</pre> - -<p>该正则匹配返回的数组包含以下属性和元素:</p> - -<table class="fullwidth-table"> - <tbody> - <tr> - <td class="header">属性/元素</td> - <td class="header">说明</td> - <td class="header">示例</td> - </tr> - <tr> - <td><code>input</code></td> - <td>只读属性,原始字符串</td> - <td>cdbBdbsbz</td> - </tr> - <tr> - <td><code>index</code></td> - <td>只读属性,匹配到的子串在原始字符串中的索引</td> - <td>1</td> - </tr> - <tr> - <td><code>[0]</code></td> - <td>只读元素,本次匹配到的子串</td> - <td>dbBd</td> - </tr> - <tr> - <td><code>[1], ...[n]</code></td> - <td>只读元素,正则表达式中所指定的分组所匹配到的子串,其数量由正则中的分组数量决定,无最大上限</td> - <td>[1]: bB<br> - [2]: d</td> - </tr> - </tbody> -</table> - -<h2 id="属性">属性</h2> - -<div><code>Array.length</code></div> - -<dl> - <dd><code>Array</code> 构造函数的 length 属性,其值为1(注意该属性为静态属性,不是数组实例的 length 属性)。</dd> - <dt>{{jsxref("Array.@@species", "get Array[@@species]")}}</dt> - <dd>返回 <code>Array</code> 构造函数。</dd> - <dt>{{jsxref("Array.prototype")}}</dt> - <dd>通过数组的原型对象可以为所有数组对象添加属性。</dd> -</dl> - -<h2 id="方法">方法</h2> - -<dl> - <dt>{{jsxref("Array.from()")}}</dt> - <dd>从类数组对象或者可迭代对象中创建一个新的数组实例。</dd> - <dt>{{jsxref("Array.isArray()")}}</dt> - <dd>用来判断某个变量是否是一个数组对象。</dd> - <dt>{{jsxref("Array.of()")}}</dt> - <dd>根据一组参数来创建新的数组实例,支持任意的参数数量和类型。</dd> -</dl> - -<h2 id="数组实例"><font face="Consolas, Liberation Mono, Courier, monospace">数组实例</font></h2> - -<p>所有数组实例都会从 {{jsxref("Array.prototype")}} 继承属性和方法。修改 <code>Array</code> 的原型会影响到所有的数组实例。</p> - -<h3 id="属性_2">属性</h3> - -<div>{{page('/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype', 'Properties')}}</div> - -<h3 id="方法_2">方法</h3> - -<h4 id="修改器方法">修改器方法</h4> - -<p>{{page('zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype', 'Mutator_methods')}}</p> - -<h4 id="访问方法">访问方法</h4> - -<p>{{page('zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype', 'Accessor_methods')}}</p> - -<h4 id="迭代方法">迭代方法</h4> - -<p>{{page('zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype', 'Iteration_methods')}}</p> - -<h2 id="数组泛型方法">数组泛型方法</h2> - -<div class="warning"> -<p><strong>泛型方法是非标准,并且已弃用,有可能不久就会移除。</strong> 需注意的是此方法同时有跨浏览器问题. 但是 <a href="https://github.com/plusdude/array-generics">Github上有可用的shim</a>。</p> -</div> - -<p>有时我们会希望在字符串或其他类数组对象上使用数组所提供的方法(如函数的 {{jsxref("Functions/arguments", "arguments", "", 1)}})。此时你可以把一个字符串作为一个字符数组来看待(也就是说,把非数组以某种方式看成是一个数组)。比如,可以用下面的方法来检查变量 <code>str</code> 中的字符是否都是字母:</p> - -<pre class="brush: js">function isLetter(character) { - return character >= 'a' && character <= 'z'; -} - -if (Array.prototype.every.call(str, isLetter)) { - console.log("The string '" + str + "' contains only letters!"); -}</pre> - -<p>这种方法能够行得通,但不够简洁,JavaScript 1.6 中引入了一个泛型化的简写形式:</p> - -<pre class="brush: js">if (Array.every(str, isLetter)) { - console.log("The string '" + str + "' contains only letters!"); -}</pre> - -<p>{{jsxref("Global_Objects/String", "String")}} 对象也包含一些泛型方法,见: {{jsxref("Global_Objects/String", "Generics", "#String_generic_methods", 1)}}。</p> - -<p>注意,这些并不属于 ECMAScript 标准,也不能在非 Gecko 浏览器中使用。你可以用标准方法 {{jsxref("Array.from()")}} 来替代上面的写法, <code>from</code> 方法可以将一个对象转换为真正的数组(虽然老的浏览器可能不支持):</p> - -<pre class="brush: js"><code>if (Array.from(str).every(isLetter)) { - console.log("The string '" + str + "' contains only letters!"); -}</code></pre> - -<h2 id="示例">示例</h2> - -<h3 id="创建数组">创建数组</h3> - -<p>下面这个例子创建了一个长度为 0 的数组 <code>msgArray</code>,然后给 <code>msgArray[0]</code> 和 <code>msgArray[99]</code> 赋值,从而导致数组长度变为了 100。</p> - -<pre class="brush: js">var msgArray = []; -msgArray[0] = 'Hello'; -msgArray[99] = 'world'; - -if (msgArray.length === 100) { - console.log('The length is 100.'); -}</pre> - -<h3 id="创建二维数组">创建二维数组</h3> - -<p>下面的例子创建了一个代表国际象棋棋盘的二维数组,然后将 (6, 4) 上的 Pawn (卒)拷贝到 (4, 4) 位置,而原本 (6, 4) 位置则被设置为空格。</p> - -<pre class="brush: js">var board = [ - ['R','N','B','Q','K','B','N','R'], - ['P','P','P','P','P','P','P','P'], - [' ',' ',' ',' ',' ',' ',' ',' '], - [' ',' ',' ',' ',' ',' ',' ',' '], - [' ',' ',' ',' ',' ',' ',' ',' '], - [' ',' ',' ',' ',' ',' ',' ',' '], - ['p','p','p','p','p','p','p','p'], - ['r','n','b','q','k','b','n','r'] ]; - -console.log(board.join('\n') + '\n\n'); - -// Move King's Pawn forward 2 -board[4][4] = board[6][4]; -board[6][4] = ' '; -console.log(board.join('\n')); -</pre> - -<p>下面是输出:</p> - -<pre class="brush: js">R,N,B,Q,K,B,N,R -P,P,P,P,P,P,P,P - , , , , , , , - , , , , , , , - , , , , , , , - , , , , , , , -p,p,p,p,p,p,p,p -r,n,b,q,k,b,n,r - -R,N,B,Q,K,B,N,R -P,P,P,P,P,P,P,P - , , , , , , , - , , , , , , , - , , , ,p, , , - , , , , , , , -p,p,p,p, ,p,p,p -r,n,b,q,k,b,n,r -</pre> - -<h3 id="用数组将一组值以表格形式显示">用数组将一组值以表格形式显示</h3> - -<pre class="brush: js"><code>values = []; -for (var x = 0; x < 10; x++){ - values.push([ - 2 ** x, - 2 * x ** 2 - ]) -}; -console.table(values)</code></pre> - -<p>结果为:</p> - -<pre><code>0 1 0 -1 2 2 -2 4 8 -3 8 18 -4 16 32 -5 32 50 -6 64 72 -7 128 98 -8 256 128 -9 512 162</code></pre> - -<p>(第一列为索引)</p> - -<h2 id="规范">规范</h2> - -<table class=" standard-table"> - <tbody> - <tr> - <th scope="col">规范</th> - <th scope="col">状态</th> - <th scope="col">说明</th> - </tr> - <tr> - <td>{{SpecName('ES1')}}</td> - <td>{{Spec2('ES1')}}</td> - <td>初始定义。</td> - </tr> - <tr> - <td>{{SpecName('ES5.1', '#sec-15.4', 'Array')}}</td> - <td>{{Spec2('ES5.1')}}</td> - <td>新增方法: {{jsxref("Array.isArray")}}, {{jsxref("Array.prototype.indexOf", "indexOf")}}, {{jsxref("Array.prototype.lastIndexOf", "lastIndexOf")}}, {{jsxref("Array.prototype.every", "every")}}, {{jsxref("Array.prototype.some", "some")}}, {{jsxref("Array.prototype.forEach", "forEach")}}, {{jsxref("Array.prototype.map", "map")}}, {{jsxref("Array.prototype.filter", "filter")}}, {{jsxref("Array.prototype.reduce", "reduce")}}, {{jsxref("Array.prototype.reduceRight", "reduceRight")}}</td> - </tr> - <tr> - <td>{{SpecName('ES6', '#sec-array-objects', 'Array')}}</td> - <td>{{Spec2('ES6')}}</td> - <td> - <p>新增方法:{{jsxref("Array.from")}}, {{jsxref("Array.of")}}, {{jsxref("Array.prototype.find", "find")}}, {{jsxref("Array.prototype.findIndex", "findIndex")}}, {{jsxref("Array.prototype.fill", "fill")}}, {{jsxref("Array.prototype.copyWithin", "copyWithin")}}</p> - </td> - </tr> - <tr> - <td>{{SpecName('ESDraft', '#sec-array-objects', 'Array')}}</td> - <td>{{Spec2('ESDraft')}}</td> - <td> - <p>新增方法:{{jsxref("Array.prototype.includes()")}}</p> - </td> - </tr> - </tbody> -</table> - -<h2 id="浏览器兼容性">浏览器兼容性</h2> - -<div>{{Compat("javascript.builtins.Array")}}</div> - -<h2 id="相关链接">相关链接</h2> - -<ul> - <li><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects#Indexing_object_properties">JavaScript Guide: “Indexing object properties”</a></li> - <li><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Predefined_Core_Objects#Array_Object">JavaScript Guide: “Predefined Core Objects: <code>Array</code> Object”</a></li> - <li><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Array_comprehensions">Array comprehensions</a></li> - <li><a href="https://github.com/plusdude/array-generics">Polyfill for JavaScript 1.8.5 Array Generics and ECMAScript 5 Array Extras</a></li> - <li><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Typed_arrays">Typed Arrays</a></li> -</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/index.md b/files/zh-cn/web/javascript/reference/global_objects/array/index.md new file mode 100644 index 0000000000..70b70221e2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/array/index.md @@ -0,0 +1,467 @@ +--- +title: Array +slug: Web/JavaScript/Reference/Global_Objects/Array +tags: + - 数组 + - 类 + - 示例 + - 全局对象 + - JavaScript + - 参考手册 +browser-compat: javascript.builtins.Array +translation_of: Web/JavaScript/Reference/Global_Objects/Array +--- +{{JSRef}} + +JavaScript的 **`Array`** 对象是用于构造数组的全局对象,数组是类似于列表的高阶对象。 + +## 描述 + +数组是一种类列表对象,它的原型中提供了遍历和修改元素的相关操作。JavaScript 数组的长度和元素类型都是非固定的。因为数组的长度可随时改变,并且其数据在内存中也可以不连续,所以 JavaScript 数组不一定是密集型的,这取决于它的使用方式。一般来说,数组的这些特性会给使用带来方便,但如果这些特性不适用于你的特定使用场景的话,可以考虑使用类型数组 {{jsxref("TypedArray")}}。 + +只能用整数作为数组元素的索引,而不能用字符串。后者称为 [关联数组](https://en.wikipedia.org/wiki/Associative_array)。使用非整数并通过 [方括号](/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects#对象和属性) 或 [点号](/zh-CN/docs/Web/JavaScript/Reference/Operators/Property_Accessors) 来访问或设置数组元素时,所操作的并不是数组列表中的元素,而是数组对象的 [属性集合](/zh-CN/docs/Web/JavaScript/Data_structures#属性) 上的变量。数组对象的属性和数组元素列表是分开存储的,并且数组的遍历和修改操作也不能作用于这些命名属性。 + +### 常见操作 + +**创建数组** + +```js +let fruits = ['Apple', 'Banana'] + +console.log(fruits.length) +// 2 +``` + +**通过索引访问数组元素** + +```js +let first = fruits[0] +// Apple + +let last = fruits[fruits.length - 1] +// Banana +``` + +**遍历数组** + +```js +fruits.forEach(function(item, index, array) { + console.log(item, index) +}) +// Apple 0 +// Banana 1 +``` + +**添加元素到数组的末尾** + +```js +let newLength = fruits.push('Orange') +// ["Apple", "Banana", "Orange"] +``` + +**删除数组末尾的元素** + +```js +let last = fruits.pop() // remove Orange (from the end) +// ["Apple", "Banana"] +``` + +**删除数组头部元素** + +```js +let first = fruits.shift() // remove Apple from the front +// ["Banana"] +``` + +**添加元素到数组的头部** + +```js +let newLength = fruits.unshift('Strawberry') // add to the front +// ["Strawberry", "Banana"] +``` + +**找出某个元素在数组中的索引** + +```js +fruits.push('Mango') +// ["Strawberry", "Banana", "Mango"] + +let pos = fruits.indexOf('Banana') +// 1 +``` + +**通过索引删除某个元素** + +```js +let removedItem = fruits.splice(pos, 1) // this is how to remove an item + +// ["Strawberry", "Mango"] +``` + +**从一个索引位置删除多个元素** + +```js +let vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot'] +console.log(vegetables) +// ["Cabbage", "Turnip", "Radish", "Carrot"] + +let pos = 1 +let n = 2 + +let removedItems = vegetables.splice(pos, n) +// this is how to remove items, n defines the number of items to be removed, +// starting at the index position specified by pos and progressing toward the end of array. + +console.log(vegetables) +// ["Cabbage", "Carrot"] (the original array is changed) + +console.log(removedItems) +// ["Turnip", "Radish"] +``` + +**复制一个数组** + +```js +let shallowCopy = fruits.slice() // this is how to make a copy +// ["Strawberry", "Mango"] +``` + +### 访问数组元素 + +JavaScript 数组的索引是从0开始的,第一个元素的索引为0,最后一个元素的索引等于该数组的 {{jsxref("Array.length", "长度")}} 减1。 + +如果指定的索引是一个无效值,JavaScript 数组并不会报错,而是会返回 `undefined`。 + +```js +let arr = ['this is the first element', 'this is the second element', 'this is the last element'] +console.log(arr[0]) // logs 'this is the first element' +console.log(arr[1]) // logs 'this is the second element' +console.log(arr[arr.length - 1]) // logs 'this is the last element' +``` + +虽然数组元素可以看做是数组对象的属性,就像 `toString` 一样,但是下面的写法是错误的,运行时会抛出 `SyntaxError` 异常,而原因则是使用了非法的属性名: + +```js +console.log(arr.0) // a syntax error +``` + +并不是 JavaScript 数组有什么特殊之处,而是因为在 JavaScript 中,以数字开头的属性不能用点号引用,必须用方括号。 + +比如,如果一个对象有一个名为 `3d` 的属性,那么只能用方括号来引用它。下面是具体的例子: + +```js +let years = [1950, 1960, 1970, 1980, 1990, 2000, 2010] +console.log(years.0) // a syntax error +console.log(years[0]) // works properly +``` + +```js +renderer.3d.setTexture(model, 'character.png') // a syntax error +renderer['3d'].setTexture(model, 'character.png') // works properly +``` + +注意在 `3d` 那个例子中,引号是必须的。你也可以将数组的索引用引号引起来,比如 `years[2]` 可以写成 `years['2']`。 + +`years[2]` 中的 2 会被 JavaScript 解释器通过调用 `toString` 隐式转换成字符串。正因为这样,`'2'` 和 `'02'` 在 `years` 中所引用的可能是不同位置上的元素。而下面这个例子也可能会打印 `true`: + +```js +console.log(years['2'] != years['02']) +``` + +### 数组长度与数字下标之间的关系 + +JavaScript 数组的 {{jsxref("Array.length", "length")}} 属性和其数字下标之间有着紧密的联系。 + +数组内置的几个方法(例如 {{jsxref("Array.join", "join")}}、{{jsxref("Array.slice", "slice")}}、{{jsxref("Array.indexOf", "indexOf")}} 等)都会考虑 {{jsxref("Array.length", "length")}} 的值。 + +另外还有一些方法(例如 {{jsxref("Array.push", "push")}}、{{jsxref("Array.splice", "splice")}} 等)还会改变 {{jsxref("Array.length", "length")}} 的值。 + +```js +const fruits = [] +fruits.push('banana', 'apple', 'peach') + +console.log(fruits.length) // 3 +``` + +使用一个合法的下标为数组元素赋值,并且该下标超出了当前数组的大小的时候,解释器会同时修改 {{jsxref("Array.length", "length")}} 的值: + +```js +fruits[5] = 'mango' +console.log(fruits[5]) // 'mango' +console.log(Object.keys(fruits)) // ['0', '1', '2', '5'] +console.log(fruits.length) // 6 +``` + +也可以显式地给 {{jsxref("Array.length", "length")}} 赋一个更大的值: + +```js +fruits.length = 10 +console.log(fruits) // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4] +console.log(Object.keys(fruits)) // ['0', '1', '2', '5'] +console.log(fruits.length) // 10 +console.log(fruits[8]) // undefined +``` + +而为 {{jsxref("Array.length", "length")}} 赋一个更小的值则会删掉一部分元素: + +```js +fruits.length = 2 +console.log(Object.keys(fruits)) // ['0', '1'] +console.log(fruits.length) // 2 +``` + +这一节的内容在 {{jsxref("Array.length")}} 中有更详细的介绍。 + +### 正则匹配结果所返回的数组 + +使用正则表达式匹配字符串可以得到一个数组。这个数组中包含本次匹配的相关信息和匹配结果。{{jsxref("RegExp.exec")}}、{{jsxref("String.match")}}、{{jsxref("String.replace")}} 都会返回这样的数组。 + +看下面的例子和例子下面的表格: + +```js +// Match one d followed by one or more b's followed by one d +// Remember matched b's and the following d +// Ignore case + +const myRe = /d(b+)(d)/i +const myArray = myRe.exec('cdbBdbsbz') +``` + +该正则匹配返回的数组包含以下属性和元素: + +<table class="fullwidth-table standard-table"> + <thead> + <tr> + <th class="header" scope="col">属性/元素</td> + <th class="header" scope="col">说明</td> + <th class="header" scope="col">示例</td> + </tr> + </thead> + <tbody> + <tr> + <td><code>input</code><br />{{ReadOnlyInline}}</td> + <td>正则表达式所匹配的原始字符串</td> + <td><code>"cdbBdbsbz"</code></td> + </tr> + <tr> + <td><code>index</code><br />{{ReadOnlyInline}}</td> + <td>匹配到的子串在原始字符串中的索引</td> + <td><code>1</code></td> + </tr> + <tr> + <td><code>[0]</code><br />{{ReadOnlyInline}}</td> + <td>最后匹配到的子串</td> + <td><code>"dbBd"</code></td> + </tr> + <tr> + <td><code>[1], ...[n]</code><br />{{ReadOnlyInline}}</td> + <td>正则表达式中所指定的分组所匹配到的子串,其数量由正则中的分组数量决定,无最大上限</td> + <td> + <code>[1]: "bB"<br />[2]: "d"</code> + </td> + </tr> + </tbody> +</table> + +## 构造器 + +- {{jsxref("Array/Array", "Array()")}} + - : 创建一个新的 `Array` 对象 + +## 静态属性 + +- {{jsxref("Array/@@species", "get Array[@@species]")}} + - : 返回 Array 的构造函数 + +## 静态方法 + +- {{jsxref("Array.from()")}} + - : 从类数组对象或者可迭代对象中创建一个新的数组实例 +- {{jsxref("Array.isArray()")}} + - : 用来判断某个变量是否是一个数组对象 +- {{jsxref("Array.of()")}} + - : 根据一组参数来创建新的数组实例,支持任意的参数数量和类型 + +## 实例属性 + +- {{jsxref("Array.prototype.length")}} + - : 数组中的元素个数 +- {{jsxref("Array/@@unscopables", "Array.prototype[@@unscopables]")}} + - : 包含了所有 ES2015 (ES6) 中新定义的、且并未被更早的 ECMAScript 标准收纳的属性名。这些属性被排除在由 [`with`](/zh-CN/docs/Web/JavaScript/Reference/Statements/with) 语句绑定的环境中 + +## 实例方法 + +- {{jsxref("Array.prototype.at()")}}{{Experimental_Inline}} + - : Returns the array item at the given index. Accepts negative integers, which count back from the last item. +- {{jsxref("Array.prototype.concat()")}} + - : 用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组 +- {{jsxref("Array.prototype.copyWithin()")}} + - : 浅复制数组的一部分到同一数组中的另一个位置,并返回它,不会改变原数组的长度 +- {{jsxref("Array.prototype.entries()")}} + - : 返回一个新的 `Array Iterator` 对象,该对象包含数组中每个索引的键/值对 +- {{jsxref("Array.prototype.every()")}} + - : 测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值 +- {{jsxref("Array.prototype.fill()")}} + - : 用一个固定值填充一个数组中从起始索引到终止索引内的全部元素 +- {{jsxref("Array.prototype.filter()")}} + - : 创建一个新数组, 其包含通过所提供函数实现的测试的所有元素 +- {{jsxref("Array.prototype.find()")}} + - : 返回数组中满足提供的测试函数的第一个元素的值。否则返回 `undefined` +- {{jsxref("Array.prototype.findIndex()")}} + - : 返回数组中满足提供的测试函数的第一个元素的**索引**。若没有找到对应元素则返回 `-1` +- {{jsxref("Array.prototype.flat()")}} + - : 按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回 +- {{jsxref("Array.prototype.flatMap()")}} + - : 使用映射函数映射每个元素,然后将结果压缩成一个新数组 +- {{jsxref("Array.prototype.forEach()")}} + - : 对数组的每个元素执行一次给定的函数 +- {{jsxref("Array.prototype.includes()")}} + - : 判断一个数组是否包含一个指定的值,如果包含则返回 `true`,否则返回 `false` +- {{jsxref("Array.prototype.indexOf()")}} + - : 返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回 `-1` +- {{jsxref("Array.prototype.join()")}} + - : 将一个数组的所有元素连接成一个字符串并返回这个字符串 +- {{jsxref("Array.prototype.keys()")}} + - : 返回一个包含数组中每个索引键的 `Array Iterator` 对象 +- {{jsxref("Array.prototype.lastIndexOf()")}} + - : 返回指定元素在数组中的最后一个的索引,如果不存在则返回 `-1` +- {{jsxref("Array.prototype.map()")}} + - : 返回一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值 +- {{jsxref("Array.prototype.pop()")}} + - : 从数组中删除最后一个元素,并返回该元素的值 +- {{jsxref("Array.prototype.push()")}} + - : 将一个或多个元素添加到数组的末尾,并返回该数组的新长度 +- {{jsxref("Array.prototype.reduce()")}} + - : 对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值 +- {{jsxref("Array.prototype.reduceRight()")}} + - : 接受一个函数作为累加器(accumulator)和数组的每个值(从右到左)将其减少为单个值 +- {{jsxref("Array.prototype.reverse()")}} + - : 将数组中元素的位置颠倒,并返回该数组。该方法会改变原数组 +- {{jsxref("Array.prototype.shift()")}} + - : 从数组中删除第一个元素,并返回该元素的值 +- {{jsxref("Array.prototype.slice()")}} + - : 提取源数组的一部分并返回一个新数组 +- {{jsxref("Array.prototype.some()")}} + - : 测试数组中是不是至少有一个元素通过了被提供的函数测试 +- {{jsxref("Array.prototype.sort()")}} + - : 对数组元素进行原地排序并返回此数组 +- {{jsxref("Array.prototype.splice()")}} + - : 通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容 +- {{jsxref("Array.prototype.toLocaleString()")}} + - : 返回一个字符串表示数组中的元素。数组中的元素将使用各自的 {{jsxref("Object.prototype.toLocaleString()")}} 方法转成字符串 +- {{jsxref("Array.prototype.toString()")}} + - : 返回一个字符串表示指定的数组及其元素。数组中的元素将使用各自的 {{jsxref("Object.prototype.toString()")}} 方法转成字符串 +- {{jsxref("Array.prototype.unshift()")}} + - : 将一个或多个元素添加到数组的头部,并返回该数组的新长度 +- {{jsxref("Array.prototype.values()")}} + - : 返回一个新的 `Array Iterator 对象`,该对象包含数组每个索引的值 +- {{jsxref("Array.prototype.@@iterator()", "Array.prototype[@@iterator]()")}} + - : 返回一个新的 `Array Iterator 对象`,该对象包含数组每个索引的值 + +## 示例 + +### 创建数组 + +下面这个例子创建了一个长度为 `0` 的数组 `msgArray`,然后给 `msgArray[0]` 和 `msgArray[99]` 赋值,从而导致数组长度变为了 `100`。 + +```js +let msgArray = [] +msgArray[0] = 'Hello' +msgArray[99] = 'world' + +if (msgArray.length === 100) { + console.log('The length is 100.') +} +``` + +### 创建二维数组 + +下面的例子创建了一个代表国际象棋棋盘的二维数组,然后将 `[6][4]` 上的 `p` (Pawn 兵) 拷贝到 `[4][4]`,而原本的 `[6][4]` 位置则被设置为空格。 + +```js +let board = [ + ['R','N','B','Q','K','B','N','R'], + ['P','P','P','P','P','P','P','P'], + [' ',' ',' ',' ',' ',' ',' ',' '], + [' ',' ',' ',' ',' ',' ',' ',' '], + [' ',' ',' ',' ',' ',' ',' ',' '], + [' ',' ',' ',' ',' ',' ',' ',' '], + ['p','p','p','p','p','p','p','p'], + ['r','n','b','q','k','b','n','r'] ] + +console.log(board.join('\n') + '\n\n') + +// Move King's Pawn forward 2 +board[4][4] = board[6][4] +board[6][4] = ' ' +console.log(board.join('\n')) +``` + +下面是输出: + +```plain +R,N,B,Q,K,B,N,R +P,P,P,P,P,P,P,P + , , , , , , , + , , , , , , , + , , , , , , , + , , , , , , , +p,p,p,p,p,p,p,p +r,n,b,q,k,b,n,r + +R,N,B,Q,K,B,N,R +P,P,P,P,P,P,P,P + , , , , , , , + , , , , , , , + , , , ,p, , , + , , , , , , , +p,p,p,p, ,p,p,p +r,n,b,q,k,b,n,r +``` + +### 用数组将一组值以表格形式显示 + +```js +values = [] +for (let x = 0; x < 10; x++){ + values.push([ + 2 ** x, + 2 * x ** 2 + ]) +} +console.table(values) +``` + +结果为: + +```plain +// The first column is the index +0 1 0 +1 2 2 +2 4 8 +3 8 18 +4 16 32 +5 32 50 +6 64 72 +7 128 98 +8 256 128 +9 512 162 +``` + +## 规范 + +{{Specifications}} + +## 浏览器兼容性 + +{{Compat}} + +## 相关链接 + +- JavaScript 指南: + + - [对象属性索引](/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects#对象属性索引) + - [索引集合类: 数组对象](/zh-CN/docs/Web/JavaScript/Guide/Indexed_collections#数组对象array_object) + +- [Typed Arrays](/zh-CN/docs/Web/JavaScript/Typed_arrays) +- [RangeError: invalid array length](/zh-CN/docs/Web/JavaScript/Reference/Errors/Invalid_array_length) diff --git a/files/zh-cn/web/javascript/reference/statements/import/index.html b/files/zh-cn/web/javascript/reference/statements/import/index.html index b7634d9cef..203ce9b8cc 100644 --- a/files/zh-cn/web/javascript/reference/statements/import/index.html +++ b/files/zh-cn/web/javascript/reference/statements/import/index.html @@ -21,7 +21,7 @@ translation_of: Web/JavaScript/Reference/Statements/import <p>语法</p> -<pre class="syntaxbox notranslate">import <em>defaultExport</em> from "<em>module-name</em>"; +<pre class="syntaxbox brush: js notranslate">import <em>defaultExport</em> from "<em>module-name</em>"; import * as <em>name</em> from "<em>module-name</em>"; import { <em>export </em>} from "<em>module-name</em>"; import { <em>export</em> as <em>alias </em>} from "<em>module-name</em>"; @@ -95,7 +95,7 @@ var promise = import("module-name");//这是一个处于第三阶段的提案。 <p>整个模块仅为副作用(中性词,无贬义含义)而导入,而不导入模块中的任何内容(接口)。 这将运行模块中的全局代码, 但实际上不导入任何值。</p> -<pre class="notranslate"><code>import '/modules/my-module.js';</code></pre> +<pre class="brush: js notranslate">import '/modules/my-module.js';</pre> <h3 id="导入默认值">导入默认值</h3> @@ -116,9 +116,9 @@ var promise = import("module-name");//这是一个处于第三阶段的提案。 // specific, named imports </pre> -<p>When importing a default export with {{anch("Dynamic Imports", "dynamic imports")}}, it works a bit differently. You need to destructure and rename the "default" key from the returned object.</p> +<p>当用{{anch("动态导入")}}的方式导入默认导出时,其工作方式有所不同。你需要从返回的对象中解构并重命名 "default" 键。</p> -<pre class="notranslate">(async () => { +<pre class="brush: js notranslate">(async () => { if (somethingIsTrue) { const { default: myDefault, foo, bar } = await import('/modules/my-module.js'); } @@ -140,15 +140,15 @@ var promise = import("module-name");//这是一个处于第三阶段的提案。 <p>关键字import可以像调用函数一样来动态的导入模块。以这种方式调用,将返回一个 <code>promise</code>。</p> -<pre class="brush: js notranslate"><code>import('/modules/my-module.js') +<pre class="brush: js notranslate">import('/modules/my-module.js') .then((module) => { // Do something with the module. - });</code> + }); </pre> <p>这种使用方式也支持 <code>await</code> 关键字。</p> -<pre class="brush: js notranslate"><code>let module = await import('/modules/my-module.js');</code></pre> +<pre class="brush: js notranslate">let module = await import('/modules/my-module.js');</pre> <h2 id="示例">示例</h2> @@ -227,6 +227,6 @@ for (const link of document.querySelectorAll("nav > a")) { <li>Limin Zhu, Brian Terlson and Microsoft Edge Team: <a href="https://blogs.windows.com/msedgedev/2016/05/17/es6-modules-and-beyond/">Previewing ES6 Modules and more from ES2015, ES2016 and beyond</a></li> <li>Hacks blog post by Jason Orendorff: <a href="https://hacks.mozilla.org/2015/08/es6-in-depth-modules/">ES6 in Depth: Modules</a></li> <li>Hacks blog post by Lin Clark: <a href="https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/">ES modules: A cartoon deep-dive</a></li> - <li>Axel Rauschmayer's book: <a class="external" href="http://exploringjs.com/es6/ch_modules.html">"Exploring JS: Modules"</a></li> + <li>Axel Rauschmayer's book: <a class="external" href="https://exploringjs.com/es6/ch_modules.html">"Exploring JS: Modules"</a></li> <li>The Modern JavaScript Tutorial(javascript.info): <a class="external" href="https://javascript.info/import-export">Export and Import</a></li> </ul> diff --git a/files/zh-cn/web/javascript/reference/statements/return/index.html b/files/zh-cn/web/javascript/reference/statements/return/index.html index 9debeab7af..8c82917d96 100644 --- a/files/zh-cn/web/javascript/reference/statements/return/index.html +++ b/files/zh-cn/web/javascript/reference/statements/return/index.html @@ -69,11 +69,11 @@ a + b;</pre> <pre class="brush: js">function counter() { for (var count = 1; ; count++) { // 无限循环 console.log(count + "A"); // 执行5次 - if (count === 5) { - return; - } - console.log(count + "B"); // 执行4次 + if (count === 5) { + return; } + console.log(count + "B"); // 执行4次 + } console.log(count + "C"); // 永远不会执行 } diff --git a/files/zh-cn/web/media/autoplay_guide/index.html b/files/zh-cn/web/media/autoplay_guide/index.html index aa8c207ed2..332f54bfa4 100644 --- a/files/zh-cn/web/media/autoplay_guide/index.html +++ b/files/zh-cn/web/media/autoplay_guide/index.html @@ -4,7 +4,7 @@ slug: Web/Media/Autoplay_guide translation_of: Web/Media/Autoplay_guide original_slug: Web/媒体/Autoplay_guide --- -<p>Automatically starting the playback of audio (or videos with audio tracks) immediately upon page load can be an unwelcome surprise to users. While autoplay of media serves a useful purpose, it should be used carefully and only when needed. In order to give users control over this, browsers often provide various forms of autoplay blocking. <span class="seoSummary">In this guide, we'll cover autoplay functionality in the various media and Web Audio APIs, including a brief overview of how to use autoplay and how to work with browsers to handle autoplay blocking gracefully.</span></p> +<p>网页加载完成后立即播放音频(或带有音频轨道的视频)可能会意外地打扰到用户。尽管自动播放媒体文件是一个很实用的功能,但是我们也应该谨慎地使用它,保证只有在它被需要的时候才使用。为了让用户拥有控制权,通常浏览器会提供各种方式禁用自动播放音频功能。<span class="seoSummary">在这篇文章中,我们将介绍各种媒体和 Web Audio APIs 的自动播放功能,包括关于如何使用自动播放功能、如何优雅的处理阻止自动播放功能的一些简短的介绍。</span></p> <p>Autoplay blocking is <em>not</em> applied to {{HTMLElement("video")}} elements when the source media does not have an audio track, or if the audio track is muted. Media with an active audio track are considered to be <strong>audible</strong>, and autoplay blocking applies to them. <strong>Inaudible</strong> media are not affected by autoplay blocking.</p> |