prettyPrint()只有一次效果.(谷歌-美化)

Sam*_*sen 1 javascript jquery prettify modal-dialog

我有这个JavaScript(使用jQuery):

var g_files_added, socket, cookie, database = null;
var file_contents = [];
function viewFile(key, filename) {
    $('#title-filename').text(filename);
    $('.prettyprint').text(file_contents[key]);
    $('#viewFileModal').modal('show');
}
$(document).ready(function() {
    $(document).on('shown', '#viewFileModal', function(event) {
            prettyPrint();
    });
});

// Variables have been set in code not shown, irrelevant to problem.
// prettyPrint() is called everytime the #viewFileModal is shown,
// but its effect is only felt once.
Run Code Online (Sandbox Code Playgroud)

因此prettyPrint(),每次显示viewFileModal模式框(由Bootstrap提供)时都会调用它,它只是每页加载似乎只有一次效果.

prettyPrint()在模拟框出现后,我尝试注释并进入JS控制台.它确实只在第一次显示框时产生影响(每页加载).

有任何想法吗?我一直坚持这一点.我也试过把调用prettyPrint()放在viewFile函数中; 但效果是一样的.

非常感谢.

山姆.

小智 6

调用"prettyPrint()"会在对其进行美化后的名为"prettyPrinted"的PRE标记中添加一个类.下面的行将删除页面上"prettyPrinted"类的所有实例,以便prettyPrint()函数可以重新美化您的PRE标记.这可以在向DIV动态添加PRE标签的情况下完成.

$('.prettyprinted').removeClass('prettyprinted');
Run Code Online (Sandbox Code Playgroud)