javascript - 如何从 javascript 检测 MathML 标签支持 (<mfrac>,<mtable>)?

标签 javascript mathml

我可以通过以下方式检测 MathML 支持:

var e = document.createElement('div');
    e.innerHTML = '<math></math>';
    var mathMLSupported = e.firstChild && "namespaceURI" in e.firstChild && e.firstChild.namespaceURI == 'http://www.w3.org/1998/Math/MathML';

但是如何检测对 <mfrac> 的支持和<mtable>

最佳答案

使用Element#getBoundingClientRect

function hasMathMLSupport() {
  const div = document.createElement("div");
  div.innerHTML = '<math><mfrac><mn>1</mn><mn>2</mn></mfrac></math>' +
                  '<math><mn>1</mn></math>';
  document.body.appendChild(div);
  return div.firstElementChild.firstElementChild.getBoundingClientRect().height > div.lastElementChild.firstElementChild.getBoundingClientRect().height + 1;
}

console.log(hasMathMLSupport());

使用window.getCompulatedStyle

(在小米浏览器的夜间模式下不起作用,因为它会将颜色更改为 rgba(255, 255, 255, 0.5))

function hasMathMLSupport() {
  const div = document.createElement("div");
  div.innerHTML = '<math><mrow mathcolor=\"red\"><mn>1</mn></mrow></math>';
  document.body.appendChild(div);
  return window.getComputedStyle(div.firstChild.firstChild, null).color === "rgb(255, 0, 0)";
}

console.log(hasMathMLSupport());

使用 Element.querySelector(":link"):(Safari 10+、Firefox ?+)

function hasMathMLSupport() {
  const div = document.createElement("div");
  div.innerHTML = '<math><mrow href=\"https://ya.ru\"><mn>1</mn></mrow></math>';
  document.body.appendChild(div);
  return !!div.querySelector(":link");
}

console.log(hasMathMLSupport());

使用 window.MathMLElement != null(该接口(interface)已在 MathML 4 规范中添加)

关于javascript - 如何从 javascript 检测 MathML 标签支持 (<mfrac>,<mtable>)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4827044/

相关文章:

javascript - JQuery UI 选项卡不会在页面重新加载时保存所选选项卡索引

javascript - 在纯 JavaScript 中将事件绑定(bind)到对象

html - 如果从本地 js 文件加载或包含源代码,则 MathJax 不工作

android - MathJax在android的WebView中查看mathMl

选项按钮上的 JavaScript 在 cakephp3 中不起作用

javascript - 如何在 JQuery 中选择除被单击元素之外的所有类?

javascript - 在 AngularJS 中切换 View 时如何让 MathML 正确渲染

javascript - 如何增加外支架的高度?

xslt - 使用 xsl 输出数学表达式

javascript - 使用 jquery 类后退格键不起作用