【问题标题】:How to get the total number of lines with SyntaxHighlighter如何使用 SyntaxHighlighter 获取总行数
【发布时间】:2013-06-21 01:37:34
【问题描述】:

是否可以使用 SyntaxHighlighter (http://alexgorbatchev.com/SyntaxHighlighter/) 获得源代码的总行数?

我可以使用这里定义的技术:How to get the number of lines in a textarea? 但也许 SyntaxHighlighter 可以更轻松地做到这一点。

谢谢。

【问题讨论】:

    标签: javascript textarea syntax-highlighting syntaxhighlighter


    【解决方案1】:

    我不相信有内置的解决方案,但这里有一个功能应该可以解决问题。它使用getElementsByClassName 方法,所以我认为它不适用于IE8 或更低版本。如果你愿意,可以使用你最喜欢的 DOM 查询库。

    /**
     * Returns the number of lines in a SyntaxHighlighter code block.
     *
     * @param {Element} node The top-level DOM element containing the code block.
     * @return {Number} The number of code lines, or 0 if not found.
     */
    function getLineCount(node) {
        var codeNode;
        var containerNode;
    
        if (node && typeof node.getElementsByClassName === 'function') {
            codeNode = node.getElementsByClassName('code');
    
            if (codeNode.length) {              
                containerNode = codeNode[0].getElementsByClassName('container');
    
                if (containerNode.length) {
                    return containerNode[0].children.length;
                }
            }
        }
    
        return 0;
    }
    

    jQuery 版本,因为显然这是一回事。

    function getLineCount(node) {
        return $(node).find('.code .container').children().length;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 2019-08-08
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多