在调用函数时在innerHTML属性中显示MathJax。

huangapple go评论58阅读模式
英文:

Displaying MathJax in innerHTML attribute calling from a function

问题

考虑我现在有的代码:

<!DOCTYPE html>
<html>
<body>

<h1>选择元素</h1>
<p id="text"></p>

</body>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>
    document.getElementById("text").innerHTML="$$你好$$";
</script>
</html>

这段代码正常运行,会在text标签中以MathJax格式显示文本"你好"。但是,如果我在这里引入一个函数,比如:

<!DOCTYPE html>
<html>
<body>

<h1>选择元素</h1>
<button onclick="start()">点击</button>
<p id="text"></p>

</body>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>
function start(){
    document.getElementById("text").innerHTML="$$你好$$";
}
</script>
</html>

点击带有"点击"文字的按钮后,只会在text标签中显示文本$$你好$$。那么,我该如何更改它?实际上,为什么会发生这种情况?

英文:

Consider the code I have:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;

&lt;h1&gt;The select element&lt;/h1&gt;
&lt;p id=&quot;text&quot;&gt;&lt;/p&gt;

&lt;/body&gt;
&lt;script id=&quot;MathJax-script&quot; async src=&quot;https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
    document.getElementById(&quot;text&quot;).innerHTML=&quot;$$hello$$&quot;;
&lt;/script&gt;
&lt;/html&gt;

It woks fine and shows the text hello in MathJax format in text id paragraph. But, if I introduce here a function, say:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;

&lt;h1&gt;The select element&lt;/h1&gt;
&lt;button onclick=&quot;start()&quot;&gt;Click&lt;/button&gt;
&lt;p id=&quot;text&quot;&gt;&lt;/p&gt;

&lt;/body&gt;
&lt;script id=&quot;MathJax-script&quot; async src=&quot;https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
function start(){
    document.getElementById(&quot;text&quot;).innerHTML=&quot;$$hello$$&quot;;}
&lt;/script&gt;
&lt;/html&gt;

Clicking the button that has click written on it, only shows the text $$hello$$ in the text tag. Now, how do I change it? Actually, why do such thing happen?

答案1

得分: 2

Mathjax不处理DOM更改,因为您在点击事件中插入HTML内容,所以需要通知MathJax DOM已更改,以便它可以查看DOM并呈现数学内容,您需要调用Mathjax的typeset()函数来执行此操作,如下所示:

function start(){
    document.getElementById("text").innerHTML="$$hello$$";
    // 调用typeset
    MathJax.typeset();
}

typeset()同步运行,查找DOM中的任何未处理的数学内容,并对该内容进行排版。

英文:

Mathjax doesnot handle DOM changes, since you are inserting HTML content on click event, you need to inform MathJax that DOM has changed so that it can look into the DOM and render the mathematical content, you need to call Mathjax's typeset() function to do that, like:

..
function start(){
    document.getElementById(&quot;text&quot;).innerHTML=&quot;$$hello$$&quot;;
    // call the typeset
    MathJax.typeset();
}
..

typeset() runs synchronously lookin for any unprocessed math content in the DOM and typesets that content.

huangapple
  • 本文由 发表于 2023年6月22日 18:24:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定