SyntaxHighlighter.all() 在 DOM 级别不起作用?[复制]
-
21-09-2019 - |
题
可能的重复:
jquery加载问题
我使用 jQuery load() 函数将内容动态加载到 div 中。在回调中,我正在调用SyntaxHighlighter.all(),以打印刚被加载到Div的Pre块的语法。
问题是内容加载正常,但语法没有突出显示。但是,当我对 div 中的 pre 块进行硬编码时,因此不通过 jQUery load() 函数加载到 DOM 中,语法 get 会按其应有的方式突出显示。
所以我猜测 SyntaxHighlighter.all() 只适用于 html 源代码中的预块,可以使用视图页面源代码进行查看,而不适用于 DOM 中的实际内容?
知道我怎样才能让它发挥作用吗?
执行加载和突出显示的 JavaScript:
<script type="text/javascript">
$.ajaxSetup ({
cache: false
});
$(document).ready(function() {
var tree = $("#tree li");
var contentContainer = $("#contentContainer");
var content = $("#content");
SyntaxHighlighter.config.clipboardSwf = 'syntaxhighlighter_2.0.320/scripts/clipboard.swf';
SyntaxHighlighter.all();
// Treeview
$("#tree").treeview({
persist: "location",
collapsed: true
});
tree.click(function() {
if ($(this).hasClass("file")) {
tree.removeClass("selected");
$(this).addClass("selected");
content.load("content/"+this.id+".html", function() {
contentContainer.effect("highlight");
SyntaxHighlighter.all();
});
}
});
});
</script>
内容分区:
<div id="content">
<pre class="brush: java;">
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
</pre>
</div>
使用 jQuery.load() 加载的外部文件:
Hello World
<pre class="brush: java;">
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
</pre>
亲切的问候
解决方案
溶液: jquery的负载问题
其他提示
- 使用 css 隐藏元素(display:none)
- 将元素添加到页面
- 调用 SyntaxHighlighter.all()
- 当您准备好使其可见时,请删除使其隐藏的 CSS 或类。
不隶属于 StackOverflow