Can i make Prettify work on any code block? without the class prettyprint?
i am using 开发者_如何学Goprettify, i am wondering if i can make it work with any code
block, not requiring the prettyprint
class.
else, how can i attach the class prettyprint
dynamically, maybe using jquery. what i want to acheive is similar to stack overflow where code typed in the editor will be "pretty printed" in the preview and output.
i tried
$("#main").delegate("code", "ready", function() {
// this does not seem to run at all?
// intending to add the prettyprint class here
});
$(document).ready(function(){ $('code').addClass('prettyprint'); });
$(document).ready(<func>)
runs<func>
when the DOM is ready.$('code')
selects all code tags..addClass()
adds the specified class to any elements it is passed (in this case, all of thecode
tags).
I don't think delegate()
is necessary here. You just need to execute prettyPrint()
every now and then to evaluate your preview. One solution might be to use setInterval
to apply Prettify after every x seconds. Here's a quick example:
$('textarea').bind('keyup', function(e) {
val = this.value.replace(/<code>/gi, '<code class="prettyprint">');
$('#preview').html(val);
});
setInterval(function(){ prettyPrint(); }, 10000);
This would execute prettyPrint()
every 10 seconds. It's not what I'd call perfect, but I think it does what you want. You'd probably also want to clear the interval if the user hasn't typed anything for a certain amount of time, but I hope this is enough to get you moving in the right direction.
精彩评论