HTML5 MathML元素使用详解

MathML(英文全称:Mathematical Markup Language)是数学标记语言,是一种基于XML(标准通用标记语言的子集)的标准,用来在互联网上书写数学符号和公式的置标语言。

MathML的顶级元素是<math>。所有有效的MathML实例必须被包括在 <math> 标记中。另外不可以在一个 <math> 元素中嵌套第二个 <math> 元素,但是 <math> 元素中可以有任意多的子元素 。

HTML5 可以在文档中使用 MathML 元素,对应的标签是 <math>…</math> 。

提示:大部分浏览器都支持 MathML 标签,如果你的浏览器不支持该标签,可以使用最新版的 Firefox 或 Safari 浏览器查看。


MathML 实例

以下是一个简单的 MathML 实例:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>奔月教程(runoon.com)</title>
    </head>
   <body>
      <math xmlns="http://www.w3.org/1998/Math/MathML">
         <mrow>
            <msup><mi>a</mi><mn>2</mn></msup>
            <mo>+</mo>		
            <msup><mi>b</mi><mn>2</mn></msup>
            <mo>=</mo>	
            <msup><mi>c</mi><mn>2</mn></msup>
         </mrow>	
      </math>
   </body>
</html>

效果展示:

奔月教程(runoon.com) a2 + b2 = c2

亲自试试


以下实例添加了一些运算符:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>奔月教程(runoon.com)</title>
    </head>
    <body>
        <math xmlns="http://www.w3.org/1998/Math/MathML">
            <mrow>
                <mrow>
                    <msup>
                        <mi>x</mi>
                        <mn>2</mn>
                    </msup>
                    <mo>+</mo>
                    <mrow>
                        <mn>4</mn>
                        <mo>⁢</mo>
                        <mi>x</mi>
                    </mrow>
                    <mo>+</mo>
                    <mn>4</mn>
               </mrow>
               <mo>=</mo>
               <mn>0</mn>
            </mrow>
        </math>
    </body>
</html>

效果展示:

奔月教程(runoon.com) x 2 + 4 x + 4 = 0

亲自试试


以下实例是一个 2×2 矩阵,可以在 Firefox 3.5 以上版本查看到效果:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>奔月教程(runoon.com)</title>
    </head>
   <body>
      <math xmlns="http://www.w3.org/1998/Math/MathML">
         <mrow>
            <mi>A</mi>
            <mo>=</mo>
            <mfenced open="[" close="]">
               <mtable>
                  <mtr>
                     <mtd><mi>x</mi></mtd>
                     <mtd><mi>y</mi></mtd>
                  </mtr>		
                  <mtr>
                     <mtd><mi>z</mi></mtd>
                     <mtd><mi>w</mi></mtd>
                  </mtr>
               </mtable>
            </mfenced>
         </mrow>
      </math>
   </body>
</html>

效果展示:

奔月教程(runoon.com) A = x y z w

亲自试试


浏览器支持

元素Google ChromeInternet ExplorerFirefoxSafariOpera
MathMLNoNoYesYesNo

目前,只有少数几个浏览器可以提供MathML全面支持,FireFox支持比较完善,Safari支持较好,其它浏览器仅能提供部分支持。


相关