【发布时间】:2018-10-26 06:03:26
【问题描述】:
我的网站使用 javascript 函数将数学标记加载(通过 AJAX)到 DOM 中,然后使用 MathJAX 渲染它。
如果该函数被第二次调用(即,将一些新的数学标记加载到 DOM 元素中):
如果 MathJAX 在第二次函数调用之前完成了其排版队列的处理,则一切正常。
-
如果 MathJAX 在第二次函数调用之前尚未完成其排版队列的处理,则会出现错误。
MathJax.js?config=TeX-MML-AM_CHTML&latest:19 Uncaught TypeError: Cannot read property 'contains' of null
一旦发生错误,MathJAX 就会处于错误状态,并且在完全重新加载(例如,通过页面重新加载)之前无法运行。
似乎最简单的解决方案是在通过 AJAX 加载新数学之前清除 MathJAX 排版队列,但我在 MathJAX API 中找不到清除排版队列的方法。
这是我正在使用的函数的精简版本(使用 jQuery 语法):
window.loadNewMath = function() {
// I want to clear the MathJAX Typeset queue before making the AJAX call
$.ajax({
dataType: "json",
type: "POST",
url: "/getNewMath",
data: [],
success: function(data) {
if (data['success']) {
// load math into DOM
$("#math-panel").html(data.new_math);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, 'math-panel']);
} else {
$("#math-panel").html('oops. there was a database error');
}
},
error: function(data) {
$("#math-panel").html('oops. there was an ajax error');
}
})
}
这种情况似乎是一个常见的用例,所以我希望有人能推荐一种避免这个错误的方法。
【问题讨论】:
标签: javascript mathjax