简体   繁体   English

如何使用javascript按钮将mathjax方程式添加到html页面中

[英]How to add a mathjax equation into the html page using a javascript button

Say I wanted to generate a random fraction on a webpage using MathJax, I might write this: 假设我想使用MathJax在网页上生成随机分数,我可能会这样写:

function newFrac() {
    a1 = ran(1,20);
    a2 = ran(1,20);
    var txt = "$ \frac{"+a1+"}{"+a2+"} $"
    document.getElementById("a").innerHTML=txt;
}

where ran(1,20) calls a function to generate a random number. 其中ran(1,20)调用函数生成随机数。 Then when the user clicked the button to make a new fraction it would write, say, $ \\frac{3}{7} $ on my webpage, but I don't want that, I want the equation displayed. 然后,当用户点击按钮创建一个新分数时,它会在我的网页上写下$ \\frac{3}{7} $ ,但我不希望这样,我希望显示等式。 How would I do that? 我该怎么办? How would I tell it to update after the javascript had changed the html? 在javascript更改了html之后,我如何告诉它更新?

To evaluate math on the page after MathJax has processed it, you need to queue a call to Typeset via MathJax.Hub.Queue(["Typeset", MathJax.Hub, el]) where el is the element containing the math that needs to be evaluated: 要在MathJax处理完数据后对页面上的数学进行评估,您需要通过MathJax.Hub.Queue(["Typeset", MathJax.Hub, el])将调用Typeset队列MathJax.Hub.Queue(["Typeset", MathJax.Hub, el])其中el是包含所需数学的元素被评估:

function newFrac() {
    a1 = ran(1,20);
    a2 = ran(1,20);
    var txt = "$ \frac{"+a1+"}{"+a2+"} $"
    var el = document.getElementById("a");
    el.innerHTML=txt;
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, el]);
}

See Modifying Math on the Page for more detail. 有关详细信息,请参阅在页面上修改数学 See the section on Manipulating Individual Math Elements if you want to change an existing equation. 如果要更改现有等式,请参阅“ 操作单个数学元素 ”一节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM