javascript - 如何使用 MathJax.typeset() 解析动态加载的内容?

标签 javascript html jquery latex mathjax

我正在使用 MathJax 来解析一些 LaTeX 方程。 下面一切正常:

<head>
<script type="text/javascript" id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js"></script>
</head>
<body>
<div id="question">
            We like,  $$e = mc^2$$
</div>
</body>

问题出现了,因为我没有硬编码 #question 的内容,而是从另一个文件加载它们。

<head>
<script type="text/javascript" id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js"></script>
<script src="jquery-cdn"></script>
<script src="questionLoader.js"></script>
</head>
<body>
<div id="question">
</div>
</body>
---------------
//This is questionLoader.js
$(document).ready(function() { 
    var theQuestion = "question1.html"; //file to load   
    $("#question").load(theQuestion);    
  });
--------------
//This is question1.html
We like,  $$e = mc^2$$

现在,有时 LaTeX 会被正确解析,有时则不会(更多时候是未解析而不是已解析)。我推测如果 MathJax 解析在 QuestionLoader 完成加载之前完成,则加载的 LaTeX 不会被解析。

有没有办法确保 QuestionLoader 在 MathJax 完成解析之前先完成加载?或者加载问题后回调 MathJax。我应该怎么做才能确保解析?

最佳答案

加载 Math 后调用 MathJax.typeset() 函数。

来自文档:

If you are writing a dynamic web page where content containing mathematics may appear after MathJax has already typeset the rest of the page, then you will need to tell MathJax to look for mathematics in the page again when that new content is produced. To do that, you need to use the MathJax.typeset() method. This will cause MathJax to look for unprocessed mathematics on the page and typeset it, leaving unchanged any math that has already been typeset.

关于javascript - 如何使用 MathJax.typeset() 解析动态加载的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64836689/

相关文章:

javascript - 鼠标悬停时如何淡出导航栏并淡入导航栏

javascript - 单击按钮以显示模态使按钮出现模态的 "on top"

javascript - 在 Jquery UI Datepicker 中突出显示特定日期

javascript - .on ('Input' ) 动态更新和添加

html - 降低 HTML 表格行高

javascript - 保持 Logo 图像纵横比的灵活标题

javascript - 使用 David J Bradshaw 的 iframe-resizer

javascript - 转换 Json 的推荐方式/库是什么?

html - 自定义 Bootstrap 对话框

php - 如何在 PHP 请求之间存储或共享实时数据?