[英]How to get the total number of lines with SyntaxHighlighter
是否可以使用SyntaxHighlighter( http://alexgorbatchev.com/SyntaxHighlighter/ )獲得源代碼的總行數?
我可以使用這里定義的技術: 如何獲取文本區域中的行數? 但是也許SyntaxHighlighter可以更輕松地做到這一點。
謝謝。
我不相信有內置的解決方案,但是這里的功能應該可以解決問題。 它使用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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.