Use Syntax Highlighter on AJAX loaded content

强颜欢笑 提交于 2019-11-29 06:13:54

问题


How can I use Alex Gorbatchev`s Syntax Highlighter on content loaded after 'window' emits 'load' event? I am trying this way:

    $.ajax({
        url:    file,
        success: function(data) {
            $('.fileName').text(file);
            $('#fileSource > pre').text(data);
            SyntaxHighlighter.all();
        }
    });

... but it is not working. I need to mention that the ajax call can occure at any time after the page loads.

Regards


回答1:


-SyntaxHighlighter.all() binds window load. So if you want to highlight element when the page loads, then use this method.

-SyntaxHighlighter.highlight() highlights elements whenever you will call this method. So it's better you use this.

-SyntaxHighlighter.highlight() takes two parameter, both are optional.

1. Parameter globalParams:

@param {Object} globalParams, Optional parameters which override element's parameters. Only used if element is specified.

2. Parameter element:

@param {Object} element, Optional element to highlight. If none is provided, all elements in the current document are highlighted.

-For more info about this, go to syntaxhighlighter_3.0.83/src/shCore.js




回答2:


I`ve found the answer to my question in one of his examples:

    $.ajax({
        url:    file,
        success: function(code) {
            $('.fileName').text(file);
            var brush = new SyntaxHighlighter.brushes.JScript(),
                html;
            brush.init({ toolbar: false });
            html = brush.getHtml(code);
            document.getElementById('source').innerHTML = html; 
        }
    });



回答3:


just add this section

$(document).ready(function () {
        $('.code').each(function () {
            SyntaxHighlighter.all();
        });
    });


来源:https://stackoverflow.com/questions/6471526/use-syntax-highlighter-on-ajax-loaded-content

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