J'utilise Prettify de Google et Markdown et je veux chaque fois que je trouve un code pre
ajouté dans le markdown textarea
pour appeler à nouveau la fonction prettyPrint()
.
Voici mon code actuel:
if($('#wmd-preview').length > 0) {
$('#wmd-preview').on('DOMNodeInserted DOMNodeRemoved',function(){
$(this).find("pre").not('.prettyprint').addClass("prettyprint");
});
}
Mais je veux quelque chose comme:
$(this).find("pre").not('.prettyprint').addClass("prettyprint",function(){
prettyPrint();
});
Existe-t-il un moyen possible d'y parvenir?
Pour autant que je comprends, vous en avez besoin:
$(this).find("pre").not('.prettyprint').each(function(){
$(this).addClass("prettyprint");
prettyPrint();
})
Vous pouvez étendre la méthode de .addClass()
jquery pour lui permettre d'accepter une fonction de rappel:
;(function ($) {
var oAddClass = $.fn.addClass;
$.fn.addClass = function () {
for (var i in arguments) {
var arg = arguments[i];
if ( !! (arg && arg.constructor && arg.call && arg.apply)) {
setTimeout(arg.bind(this));
delete arguments[i];
}
}
return oAddClass.apply(this, arguments);
}
})(jQuery);
Ensuite, utilisez-le comme d'habitude:
$('pre:not(.prettyprint)').addClass('prettyprint',prettyPrint);
La fonction addClass
jQuery n'a pas de rappel dans les arguments.
En savoir plus à ce sujet dans documentation .
Je pense que cela pourrait fonctionner pour vous:
// prettyprint class is added
$(this).find("pre").not('.prettyprint').addClass("prettyprint");
// after prettyprint class is added, prettyPrint function is called
prettyPrint();