<semantics>

Baseline 已廣泛支援

此功能已成熟,並且在眾多裝置和瀏覽器版本上都能正常工作。自 2023 年 1 月以來,它已在各瀏覽器中可用。

<semantics> MathML 元素將註釋與 MathML 表示式相關聯,例如其作為 輕量級標記語言 的文字來源,或以特定 XML 方言表達的數學含義。通常,其結構為:

預設情況下,僅渲染 <semantics> 元素的第一個子元素,而其他元素的 display 屬性被設定為 none

css
semantics > :not(:first-child) {
  display: none;
}

注意:舊的 MathML 規範允許渲染器根據可用的註釋決定預設渲染方式。以下確定可見子元素的規則已在某些瀏覽器中實現。有關 Presentation MathML 和 Content MathML 之間的區別,請參閱 MathML 4

  • 如果沒有其他規則適用:預設情況下,僅渲染第一個子元素,該子元素應為 Presentation MathML。
  • 如果第一個子元素是 Presentation MathML 元素(非 <annotation><annotation-xml>),則渲染第一個子元素。
  • 如果沒有找到 Presentation MathML,則渲染 <semantics> 的第一個不帶 src 屬性的 <annotation><annotation-xml> 子元素。對於 <annotation-xml> 元素,encoding 屬性必須等於以下值之一:
    • "application/mathml-presentation+xml"
    • "MathML-Presentation"
    • "SVG1.1"
    • "text/html"
    • "image/svg+xml"
    • "application/xml"

請注意,這裡沒有提到 "application/mathml+xml",因為它不區分 Content MathML 或 Presentation MathML。

屬性

此元素的屬性包括 全域性 MathML 屬性

示例

html
<math display="block">
  <semantics>
    <!-- The first child is the MathML expression rendered by default. -->
    <mrow>
      <msup>
        <mi>x</mi>
        <mn>2</mn>
      </msup>
      <mo>+</mo>
      <mi>y</mi>
    </mrow>

    <!--
      Annotate with content MathML, a dedicated XML dialect
      to express the meaning of mathematical formulas.
    -->
    <annotation-xml encoding="application/mathml-content+xml">
      <apply>
        <plus />
        <apply>
          <power />
          <ci>x</ci>
          <cn type="integer">2</cn>
        </apply>
        <ci>y</ci>
      </apply>
    </annotation-xml>

    <!--
      Annotate with LaTeX, a lightweight markup language
      to write mathematical formulas.
    -->
    <annotation encoding="application/x-tex">x^{2} + y</annotation>
  </semantics>
</math>

技術摘要

隱式 ARIA 角色 None

規範

規範
MathML Core
# semantics-and-presentation

瀏覽器相容性