prettyPrint()只有一次效果。 (谷歌-美化) [英] prettyPrint() only has an effect once. (google-prettify)

查看:399
本文介绍了prettyPrint()只有一次效果。 (谷歌-美化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个JavaScript(使用jQuery):

I have this JavaScript (with 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.

所以每次调用prettyPrint()显示了viewFileModal模式框(由Bootstrap提供),它只是每页加载似乎只有一次效果。

So prettyPrint() is invoked every time the viewFileModal modal box (courtesy of Bootstrap) is shown, it's just that it only seems to have an effect once per page load.

我试过评论 prettyPrint()并在出现模式框后进入JS控制台。它确实只在第一次显示框时产生效果(每页加载)。

I have tried commenting out prettyPrint() and entering at the JS console after making the modal box appear. It indeed only has an effect the first time the box is shown (per page load).

任何想法?我一直坚持这一点。我还尝试在viewFile函数中调用 prettyPrint();但效果是一样的。

Any ideas? I have been stuck on this a while. I also tried putting the call to prettyPrint() in the viewFile function; but the effect is the same.

非常感谢。

Sam。

推荐答案

调用prettyPrint()会在对其进行美化后的名为prettyPrinted的PRE标记中添加一个类。下面的行将删除页面上prettyPrinted类的所有实例,以便prettyPrint()函数可以重新解析您的PRE标记。这可以通过 >动态地将PRE标签添加到DIV来完成。

Calling "prettyPrint()" adds a class to your PRE tags named "prettyPrinted" after it has been prettified. The line below will remove all instances of the "prettyPrinted" class on your page so that the prettyPrint() function can re-prettify you're PRE tags. This can be done without dynamically adding PRE tags to DIVs.

$('.prettyprinted').removeClass('prettyprinted');

这篇关于prettyPrint()只有一次效果。 (谷歌-美化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆