问题
I am using google-code-prettify to format some code that I am placing on my website. the code is dynamically added by me through a javascript function.
The code submits fine… and prettifies beautifully. However, in order to get it to work I have to run the prettyPrint(); function after I insert the code. When I do this the existing blocks of code get indented and they are given a new line number.
Here is what it looks like:
Newly inserted code block:
1. function test(){ document.write("hello world"); }
Existing code blocks turn into this:
1.
1. function test(){ document.write("hello world");}
Is there any way to prevent this? Possibly running the function on only the dynamically inserted code? changing the class names of the existing code blocks? (i have been unsuccessful with that maybe I am doing it wrong)
回答1:
Pass the string containing the newly inserted code that needs formatting to prettyPrintOne
.
This tutorial describes an implementation that does precisely this: Syntax Highlighting a la StackOverflow with Google Prettify .
来源:https://stackoverflow.com/questions/7110694/can-i-google-code-prettify-only-a-portion-of-the-page-run-a-javascript-functio