使用javascript和MathJax向文档中添加数学公式字符串
当按下按钮添加数学公式时,MathJax语句似乎没有被解释:使用javascript和MathJax向文档中添加数学公式字符串,javascript,html,string,mathjax,Javascript,Html,String,Mathjax,当按下按钮添加数学公式时,MathJax语句似乎没有被解释: const-button=document.getElementById('go'); 按钮。addEventListener('click',displayMath,true); 函数displayMath(){ const body=document.body; p=document.createElement('p'); 常数a=3,b=2; 常数c=a+b; 常量数学=“\\(“+a+”+“+b+”=“+c+”\)”; p、
const-button=document.getElementById('go');
按钮。addEventListener('click',displayMath,true);
函数displayMath(){
const body=document.body;
p=document.createElement('p');
常数a=3,b=2;
常数c=a+b;
常量数学=“\\(“+a+”+“+b+”=“+c+”\)”;
p、 innerHTML=数学;
体感儿童(p);
}
按下按钮显示如下数学:\(a+b=c\)
走!
您需要再次运行Mathjax解析器,将动态添加的表达式转换为math。这是。
所以,您需要做的是运行MathJax.Hub.Queue()新表达式上的代码>方法。您的代码应该如下所示:
const button = document.getElementById('go');
button.addEventListener('click', displayMath, true);
function displayMath() {
const body = document.body;
p = document.createElement('p');
const a = 3, b = 2;
const c = a + b;
const math = "\\(" + a + "+ " + b + " = " + c + "\\)";
p.innerHTML = math;
body.appendChild(p);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, p]);
}
而且非常感谢,怀疑有类似的事情,但找不到方法。令人惊叹的!