我正在尝试通过.html()更新MathJax-math,但是,似乎我的代码无法正常工作。我当前的代码看起来像这样,但是它输出“1 + 2 = 3” 和呈现:
$$\class{x}{2}+\class{y}{2}=\class{z}{5}$$
<script>
$( '.x' ).html( '1' );
$( '.y' ).html( '2' );
$( '.z' ).html( '3' );
MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
</script>
我尝试了不同的命令,但似乎没有任何作用。 [“Rerender”,MathJax.Hub]仅呈现“2 + 2 = 5”,因此似乎重置了.html():
<script>
MathJax.Hub.Queue(["Rerender",MathJax.Hub]);
</script>
所需的结果看起来像这样(省略了js),其中\ class {x} {}(及其他)可能在不同的位置出现多次:
<span>You have chosen \(\class{x}{}\) and \(\class{y}{}\)</span>
$$\class{x}{}+\class{y}{}=\class{z}{}$$
有什么办法可以这样渲染“1 + 2 = 3”吗? $('.x')可以更改多次,而不仅仅是一次。
最佳答案
弗兰克,使用以下代码:
HTML:
<html>
<head>
<script
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML">
</script>
</head>
<body>
<div id="formula"></div>
A: <input type="text" id="valueA">
B: <input type="text" id="valueB">
C: <input type="text" id="valueC">
<p><input type="button" value="Update" onclick="DynamicMJ.update()" /></p>
<script>
var DynamicMJ = {
formula: document.getElementById("formula"),
update: function () {
var a = document.getElementById("valueA").value;
b = document.getElementById("valueB").value;
var c = document.getElementById("valueC").value;
var tex = "\\frac{"+a+"}{2}+ \\frac{"+b+"}{2} = \\frac{"+c+"}{5}";
this.formula.innerHTML = "\\["+tex+"\\]";
MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.formula]);
}
};
DynamicMJ.update();
</script>
</body>
</html>
说明:
您需要使用HTML元素(在此示例中为div)来编写值,然后可以将文本框的值直接插入公式中。
希望这可以帮助!