使用MathJax将元素添加到DOM? [英] Using MathJax to add element to the DOM?
问题描述
我试图动态创建一个元素,然后对其进行一些处理.我试图在新创建的元素上使用MathJax,但无法在其上拉MathJax.Hub.getAllJax.
I am trying to dynamically create an element then do some processing on it. I was trying to use MathJax on that newly created element but I can't pull MathJax.Hub.getAllJax on it.
我在控制台中键入了以下内容以测试结果:(MathOutput1在HTML中定义)
I typed this in the console to test things out: (MathOutput1 is defined in the HTML)
MathJax.HTML.addElement(document.body, "div", {id: "blah"}, ['` `']);
<div id="blah">` `</div>
MathJax.Hub.getAllJax("blah");
[]
MathJax.Hub.getAllJax("MathOutput1")
[Object]
有没有一种方法可以动态创建可以作用的元素?
Is there a way I can dynamically create an element that I can act on?
我确实确认div元素已添加到dom中(至少根据镶边)""
I did confirm that the div element blah was added to the dom (at least according to chrome) ""
谢谢:)
推荐答案
此处的问题是 MathJax异步运行,您正在尝试在MathJax.Hub
例程完全完成之前访问新元素.
The problem here is that MathJax operates asynchronously, and you're trying to access the new element before the MathJax.Hub
routines are fully finished.
要解决此问题,您可以在Hub
的回调队列上的回调函数中将对MathJax.Hub.getAllJax()
的调用放在中:
To get around this, you can put your call to MathJax.Hub.getAllJax()
in a callback function on the Hub
's callback queue:
function showBlahElement () {
console.log(MathJax.Hub.getAllJax("blah"));
}
MathJax.HTML.addElement(document.body, "div", {id: "blah"}, ['$$a=b$$']);
MathJax.Hub.Queue(showBlahElement);
或者,您可以注册一个信号侦听器,该信号侦听器将在发生特定事件时触发(在这种情况下,每当设置了新的数学运算时):
Or, you can register a signal listener that will get triggered whenever a certain event happens (in this case, whenever new math is typeset):
MathJax.Hub.Register.MessageHook("New Math", function (message) {
console.log(MathJax.Hub.getAllJax("blah"));
});
MathJax.HTML.addElement(document.body, "div", {id: "blah"}, ['$$a=b$$']);
有关更多信息,请参见 API文档.
For more information, see the API docs.
这篇关于使用MathJax将元素添加到DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!