返回介绍

Describing HTML snippets

发布于 2019-07-04 19:44:31 字数 2698 浏览 843 评论 0 收藏 0

A number of MathJax configuration options allow you to specify an HTML snippet using a JavaScript object. This lets you include HTML in your configuration files even though they are not HTML files themselves. The format is fairly simple, but flexible enough to let you represent complicated HTML trees.

An HTML snippet is an array consisting of a series of elements that format the HTML tree. Those elements are one of two things: either a string, which represents text to be included in the snippet, or an array, which represents an HTML tag to be included. In the latter case, the array consists of three items: a string that is the tag name (e.g., “img”), an optional object that gives attributes for the tag (as described below), and an optional HTML snippet array that gives the contents of the tag.

When attributes are provided, they are given as name:value pairs, with the name giving the attribute name, and value giving its value. For example

[["img",{src:"/images/mypic.jpg"}]]

represents an HTML snippet that includes one element: an <img> tag with src set to /images/mypic.jpg. That is, this is equivalent to

<img src="/images/mypic.jpg">

Note that the snippet has two sets of square brackets. The outermost one is for the array that holds the snippet, and the innermost set is because the first (and only) element in the snippet is a tag, not text. Note that the code ["img",{src:"/images/mypic.jpg"}] is invalid as an HTML snippet. It would represent a snippet that starts with “img” as text in the snippet (not a tag), but the second item is neither a string nor an array, and so is illegal. This is a common mistake that should be avoided.

A more complex example is the following:

[
  "Please read the ",
  ["a",{href:"instructions.html"},["instructions"]],
  " carefully before proceeding"
]

which is equivalent to

please read the <a href="instructions.html">instructions</a> carefully
before proceeding.

A final example shows how to set style attributes on an object:

[["span",
  {
    id:"mySpan",
    style: {color:"red", "font-weight":"bold"}
  },
  [" This is bold text shown in red "]
]]

which is equivalent to

<span id="mySpan" style="color: red; font-weight: bold;">
This is bold text shown in red
</span>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文