如何在syntaxhighlighter中创建动态内容 [英] How to create dynamic content within syntaxhighlighter

查看:192
本文介绍了如何在syntaxhighlighter中创建动态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据用户输入显示一个属性名称,并在SyntaxHighlighter中显示它。 另一个帖子说这应该很简单。

I want to display a property name based on user input and display this inside of SyntaxHighlighter. Another post says this is supposed to be easy.

JS

JS

    $('#inputText').keyup(function () {
        var outputValue = $('#codeTemplate').html();//Take the output of codeTemplate
        $('#codeContent').html(outputValue);//Stick the contents of code template into codeContent

        var finalOutputValue = $('#codeContent').html();//Take the content of codeContent and insert it into the sample label
        $('.popover #sample').html(finalOutputValue);

        SyntaxHighlighter.highlight();
    });               

    SyntaxHighlighter.all();

标记

Markup

<div style="display: none;">
    <label class="propertyName"></label>
    <label id="codeTemplate">
        <label class="propertyName"></label>
        //Not using Dynamic object and default Section (appSettings):
        var actual = new Configuration().Get("Chained.Property.key");

        //more code
    </label>            

    <pre id="codeContent" class="brush: csharp;">

    </pre>
</div>

<div id="popover-content" style="display: none">
    <label id="sample">

    </label>
</div>

输出纯文本。仿佛SyntaxHighlighter从不跑。我怀疑这个问题与页面渲染后< pre> 不存在有关。然而,更新

This outputs plain text. As if SyntaxHighlighter never ran. I suspect that the issue has to do with the fact that <pre> doesn't exist after the page is rendered. However, updating

SyntaxHighlighter.config.tagName =label;

以及pre to label也不起作用。

along with pre to label did not work either.

推荐答案

JS

JS

<script>        
    $(function () {        
        $('#Key').popover({
            html: true,
            trigger: 'focus',
            position: 'top',
            content: function () {
                loadCodeData(true);
                console.log('content updated');
                var popover = $('#popover-content');
                return popover.html();//inserts the data into .popover-content (a new div with matching class name for the id)
            }
        });       

        $('#Key').keyup(function () {
            loadCodeData();
        });

        function loadCodeData(loadOriginal) {
            var userData = $('#Key').val();
            var codeTemplate = $('#codeTemplate').html();
            var tokenizedValue = codeTemplate.toString().replace('$$propertyNameToken', userData);
            $('#codeContent').html(tokenizedValue);
            $('#codeContent').attr('class', 'brush: csharp');//!IMPORTANT: re-append the class so SyntaxHighlighter will process the div again

            SyntaxHighlighter.highlight();

            var syntaxHighlightedResult = $('#codeContent').html();//Take the content of codeContent and insert it into the div                   

            var popover;
            if(loadOriginal)
                popover = $('#popover-content');//popover.content performs the update of the generated class for us so well we need to do is update the popover itself
            else {
                popover = $('.popover-content');//otherwise we have to update the dynamically generated popup ourselves.
            }

            popover.html(syntaxHighlightedResult);
        }

        SyntaxHighlighter.config.tagName = 'div';//override the default pre because pre gets converted to another tag on the client.  
        SyntaxHighlighter.all();
    });
</script>

标记

Markup

<div style="display: none;">
    <label id="codeTemplate">
        //Not using Dynamic object and default Section (appSettings):
        var actual = new Configuration().Get("$$propertyNameToken");

        //Using a type argument:
        int actual = new Configuration().Get&lt;int>("asdf");

        //And then specifying a Section:
        var actual = new Configuration("SectionName").Get("test");

        //Using the Dynamic Object and default Section:
        var actual = new Configuration().NumberOfRetries();

        //Using a type argument:
        int actual = new Configuration().NumberOfRetries&lt;int>();

        //And then specifying a Section:
        var actual = new Configuration("SectionName").NumberOfRetries(); 
    </label>            

    <div id="codeContent" class="brush: csharp;">

    </div>
</div>

<div id="popover-content" style="display: none">

</div>

这篇关于如何在syntaxhighlighter中创建动态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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