首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实时显示mathjax输出

实时显示mathjax输出
EN

Stack Overflow用户
提问于 2011-10-28 13:49:13
回答 3查看 3.4K关注 0票数 11

如何修改此mathjax示例以在输入时进行实时预览?现在,它只在我按回车键后才显示结果。我想调整它,使它的工作方式类似于stackoverflow/math.stackexchange在输入问题时显示预览。

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-10-28 14:14:19

尝试onkeypressonkeyup,而不是使用onchange

onchange只在您离开字段时触发,但其他(显然)在每次击键时都会触发。

票数 5
EN

Stack Overflow用户

发布于 2012-04-02 07:34:02

我怀疑您使用的是Internet Explorer,它不像其他浏览器那样频繁或高效地触发onchange事件。

MathJax Examples中的版本包含更多代码,以便更好地处理IE。您可能希望查看那里的源代码以了解详细信息。

票数 1
EN

Stack Overflow用户

发布于 2021-01-31 18:14:04

代码语言:javascript
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7925622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档