我使用knockout.js基于Ajax调用动态插入值。其中一个值,content.front,是utf8文本,它可以包含MathJax表达式。我试图弄清楚如何让MathJax在每次knockout.js更新div时呈现表达式。我不能很好地让它工作,我也不知道为什么。这里是JavaScript:
var ViewModel = function(data) {
var self = this;
self = ko.mapping.fromJS(data, {}, self);
self.content.back.subscribe( function() {
MathJax.Hub.Queue(["Typeset", MathJax.Hub,"preview-back"]);
});
}; 哈姆雷特:
<div .wrap>
<div .tab-content>
<div .tab-pane #preview-back data-bind="text: content.back">(此哈姆雷特呈现的html是有效的html 5.)
目前,knockout.js正在适当的div中呈现文本。当我修改文本时,div也会发生变化。而MathJax似乎正在被触发--“排版100%”的小弹出弹出。但是div并没有被改变。
发布于 2013-10-10 21:25:35
好吧,所以我最终做了一个定制的敲除绑定。我在这里也使用jquery:
ko.bindingHandlers.mathjax = {
update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
// This will be called once when the binding is first applied to an element,
// and again whenever the associated observable changes value.
// Update the DOM element based on the supplied values here.
var value = valueAccessor(), allBindings = allBindingsAccessor();
var valueUnwrapped = ko.unwrap(value);
// the replace is an artefact of my encoding. Maybe I will use markdown instead.
$(element).html(valueUnwrapped.replace(/\n/g, '<br>'));
MathJax.Hub.Queue(["Typeset",MathJax.Hub,element]);
}
};ViewModel保持不变,html模板是:
<div data-bind="mathjax: content.back"></div>希望这能帮到别人!
https://stackoverflow.com/questions/19304643
复制相似问题