Looking for an idea how to measure line count
I want to take a text on a webpage and measure it's height in lines, regardless of any other parameter, so if I shrink the text width and get more lines, i will get the开发者_开发知识库 new line count.
Any ideas how to do that using JS and jQuery?
Also, what kind of line numbering techniques are common?
Almost same idea as Ghommey's comment, but with line-height instead of height.
Fiddle here: http://jsfiddle.net/k_rma/vbCJm/
Here is a small jQuery function doing it:
$.fn.numLines = function() {
var tmp = $('<div style="visibility:hidden;">x</div>').appendTo(document.body);
var lines = this.height() / tmp.height();
tmp.remove();
return lines;
};
You simply call $('#someElement').numLines()
to retrieve the number of lines.
Here's a demo: http://jsfiddle.net/ThiefMaster/sHzMP/
This will give total number of line in given text
text.split("\n").length
精彩评论