可以使用SyntaxHighlighter (http://alexgorbatchev.com/SyntaxHighlighter/)获得源码的总行数吗?
我可以使用这里定义的技术:How to get the number of lines in a textarea?,但也许SyntaxHighlighter可以更容易地做到这一点。
谢谢。
发布于 2013-06-21 01:37:34
我不相信有一个内置的解决方案,但这里有一个函数应该可以做到这一点。它使用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;
}https://stackoverflow.com/questions/11934907
复制相似问题