实时显示MathJax输出

11

如何修改这个MathJax示例,使其在我输入时进行实时预览?目前只有在我按下回车键后才显示结果。我想调整它的方式,使其类似于stackoverflow/math.stackexchange在输入问题时显示预览。

<html>
<head>
<title>MathJax Dynamic Math Test Page</title>

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [["$","$"],["\\(","\\)"]]
    }
  });
</script>
<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full">
</script>

</head>
<body>

<script>
  //
  //  Use a closure to hide the local variables from the
  //  global namespace
  //
  (function () {
    var QUEUE = MathJax.Hub.queue;  // shorthand for the queue
    var math = null;                // the element jax for the math output.

    //
    //  Get the element jax when MathJax has produced it.
    //
    QUEUE.Push(function () {
      math = MathJax.Hub.getAllJax("MathOutput")[0];
    });

    //
    //  The onchange event handler that typesets the
    //  math entered by the user
    //
    window.UpdateMath = function (TeX) {
      QUEUE.Push(["Text",math,"\\displaystyle{"+TeX+"}"]);
    }
  })();
</script>

Type some TeX code:
<input id="MathInput" size="50" onchange="UpdateMath(this.value)" />
<p>

<div id="MathOutput">
You typed: ${}$
</div>

</body>
</html>

是的,因为onchange只有在按下回车键或字段失焦时才会触发。 - Lucas
未来注意事项:cdn.mathjax.org即将终止,查看https://www.mathjax.org/cdn-shutting-down/获取迁移提示。 - Peter Krautzberger
@PeterKrautzberger,在WordPress网站中,如何展示MathJax的实时预览,该网站有用于编写带有数学公式的问题/评论的输入字段。对于绝对初学者,请指导在哪里输入脚本(如果有)以及要输入什么内容。谢谢。 - user12345
@think123 在WordPress网站中,如何展示MathJax的实时预览,该网站具有用于编写带有数学公式的问题/评论的输入字段。对于绝对初学者,请指导在哪里输入脚本(如果有)以及要输入什么内容。谢谢。 - user12345
3个回答

5

不要使用 onchange,改用 onkeypressonkeyup

onchange 仅在离开字段时触发,但其他两者(显然)会随着每个按键而触发。


在WordPress网站中,如何展示MathJax的实时预览,该网站具有用于编写带有数学公式的问题/评论的输入字段?对于绝对初学者,请指导在哪里输入脚本(如果需要)以及输入什么样的脚本。谢谢。 - user12345

1

我猜你正在使用Internet Explorer,它不像其他浏览器那样频繁或高效地触发onchange事件。

MathJax Examples中的版本包含更多处理IE的代码。你可能想查看那里的源代码以获取详细信息。


这并不是真的,事实上。 - Lucas

0

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({tex2jax: {inlineMath: [ ['$','$'], ["\\(","\\)"] ],processEscapes: true}});
</script>

<script
  type="text/javascript"
  charset="utf-8"
  src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

<script>
function f() {
  var input = document.getElementById("input");
  document.getElementById("output").innerHTML = input.value;
  MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
  }
</script>

<textarea id="input" cols="25" rows="5" onkeyup="f()">
</textarea>

<p id="output"></p>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接