问题
I'm using a javascript syntax highlighter (http://alexgorbatchev.com/SyntaxHighlighter/). I think this one is very famous.
in my js, I use ajax to get code samples and then initialize syntax highlighter. When I call the same ajax call with a different parameter, it returns a different code sample. The problem is that I can't replace syntax highlighter's content. I looked up its API list, but I can't find an API to update or replace its content.
Please advise me.
回答1:
Just empty the wrapper element, put the new content inside, and then run syntax highlighter on that new code.
There shouldn't be any memory issues. Afaik, Syntax Highlighter simply creates a DOM structure (a table with DIV's, etc.) based on your code block. When you remove that table from the DOM (for instance wrapper.innerHTML = '';), all those elements are garbage collected.
来源:https://stackoverflow.com/questions/7761867/how-to-replace-content-of-javascript-syntax-highlighter