【发布时间】:2017-01-23 17:32:24
【问题描述】:
我正在使用来自 Google 和 Markdown 的 Prettify,并且我希望每次在 markdown textarea 中添加一个 pre 代码来再次调用 prettyPrint() 函数。
这是我的实际代码:
if($('#wmd-preview').length > 0) {
$('#wmd-preview').on('DOMNodeInserted DOMNodeRemoved',function(){
$(this).find("pre").not('.prettyprint').addClass("prettyprint");
});
}
但我想要类似的东西:
$(this).find("pre").not('.prettyprint').addClass("prettyprint",function(){
prettyPrint();
});
有什么办法可以做到吗?
【问题讨论】:
-
回调会做什么?什么时候调用?
-
可以看第二个例子。回调将调用函数 prettyPrint() ,该函数将样式添加到 Markdown 预览中插入的代码。每次发现没有
prettyprintcss 类的新pre元素时都应该调用它。我不想每次都调用domnodeinserted或domnoderemoved事件,但仅限于上述情况。 -
addClass 没有回调。您是说要在每个 pre 获得
prettyprint课程后运行 prettyPrint? -
@MateiMihai 你可以用你自己的覆盖
addClass函数,这将触发一个自定义事件(但这在这里没有帮助,所以......) -
It should be called each time is found a new pre element without a prettyprint你的意思是当新的pre元素添加到 DOM 时应该发生这种情况?