I'm using Prettify for syntax highlighting, but it doesn't work on dynamically generated code.
I have a form that when submitted generates code and displays it (without refreshing) in <div id="output></div>
, but prettify d开发者_开发技巧oesn't work on this code, is there any workaround?
Many thanks!
Make sure you recall Prettify once the new code is loaded.
You will need to add a handler to the 'Submit' event. I don't know if you are using a framework or raw JS, so I can't give a code example.
There's a nice solution at http://www.codingthewheel.com/archives/syntax-highlighting-stackoverflow-google-prettify.
In short:
- listen for an event: user is inactive for X seconds (after key press)
- run the Prettify function
prettyPrint()
- (if a user starts typing before X seconds do not run Prettify)
.
$(document).ready(function() {
$('#mytextarea').keydown(function() {
$(this).stopTime();
$(this).oneTime(1000, function() {
/* launch the Prettify here */
});
});
});
code copied from http://www.codingthewheel.com/archives/syntax-highlighting-stackoverflow-google-prettify
精彩评论