Can I Google-Code-Prettify only a portion of the page? (run a javascript function on a specified portion of the page)

北慕城南 提交于 2020-01-15 15:28:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!