Overview
The number of the text lines inside the Document Object Model (DOM) element whether it would be any element that is dc6dce4a544fdca2df29d5ac0ea9906b, e388a4556c0f65e1904146cc1a846bee or 2e1cf0710519d5598b1f0f14c36ba674 can be deliberate mainly in two approaches. In the first approach we will be dividing the “offsetHeight” that is height of the DOM element with the line height in output which will give the total number of lines.
在第二种方法中,我们将使用数组的split()方法,其中我们将把换行符作为参数传递。因此,数组将由换行符前后的元素创建,并且我们将计算数组的长度。
Algorithm
Step 1 − Create a HTML template, add a div element in it with line-height. Line-height is the height of the particular line. Also create a span tag in which the output will be generated.
步骤2 − 现在在脚本标签中访问行的父元素。使用offsetHeight计算父元素的总高度。
var domHeight = a.offsetHeight;
步骤 3 − 使用样式属性获取行的行高值。由于行高值还包含单位“px”,因此我们需要使用parseInt()函数从中获取数字。
第四步 − 现在将提取的值,即domHeight和line-height进行分割。
var totalLines = domHeight / linesHeight;
第5步- 变量“totalLines”包含父元素中的行数。这将返回span标签中的行数。
Example
的中文翻译为:
示例
在这个例子中,我们将通过计算整个文档对象模型(DOM)元素的高度来统计行数,使用offsetHeight属性来获取DOM元素的高度,并将DOM高度除以行高,这将给我们输出DOM中存在的总行数。
<html>
<head>
<title>Count the text lines inside of DOM element</title>
</head>
<body>
<div id="lines" style="line-height: 30px;">
Welcome to tutorialspoint<br>
Visit us at: https://www.tutorialspoint.com/<br>
Buy the course of your domain <br>
Get the certificate <br>
Thank you for visiting<br>
</div>
<strong style="border: 2px solid black; padding: 0.1rem;">
Total number of lines:
<span id="out"></span>
</strong>
<script>
var a = document.getElementById("lines");
var domHeight = a.offsetHeight;
var linesHeight = parseInt(a.style.lineHeight);
var totalLines = domHeight / linesHeight;
document.getElementById("out").innerText=totalLines;
</script>
</body>
</html>
在下面的图像中,它显示了上面示例的输出。其中包含了“30px”的行高,它将DOM元素的“offsetHeight”分割,并返回span标签中元素的数量。
Algorithm
步骤 1 − 创建一个HTML模板,在其中添加一个div元素。创建一个span标签,用于生成输出。
Step 2 − Now access the text inside the parent element using the document.getElementById().
const textLines = document.getElementById("lines").innerText;
步骤 3 − 使用数组split()方法,将换行符(“
”)作为参数传递给它。split方法将文本的行存储在数组中作为一个元素。
Step 4 − Now we will use the length method of array to calculate the length of an array which will return the number of text lines in the element.
const numLines = textLines.split("").length;
Step 5 − Display the output of the number of text lines in span tag using
document.getElementById("out").innerText=numLines-1;
在这里,我们从“numLines”中减去了一个,因为它包含了一个额外的换行元素,该元素在所有文本行开始之前被插入。
Example
的中文翻译为:
示例
In this example, we will calculate the number of lines by using the array split() method in which we will access the DOM in a variable and with the help of split(“
”) method we will pass a line break as argument which will store all the element in an array as the line breaks. After which we will calculate the length of an array, which will return the number of text lines available in the DOM.
<html>
<head>
<title>Count the text lines inside of DOM element</title>
</head>
<body>
<div id="lines" style="line-height: 30px;">
Welcome to tutorialspoint<br>
Visit us at: https://www.tutorialspoint.com/<br>
Thank you for visiting<br>
</div>
<strong style="border: 2px solid black; padding: 0.1rem;">
Total number of lines:
<span id="out"></span>
</strong>
<script>
const textLines = document.getElementById("lines").innerText;
const numLines = textLines.split("").length;
document.getElementById("out").innerText=numLines-1;
</script>
</body>
</html>
In the below image, it shows the output of the above example. In which all the three text lines are stored in the array as an element. So on calculating the length of an array it will return 3.
结论
In the first example we had to parse the value of the line-height with parseInt() because it contains the string value also and if we divide that value from the “offsetHeight” value then it will return (NaN) which means “Not A Number”. So we had used parseInt(), but we can also use parseFloat() if we want to return it in decimal.
以上就是如何计算DOM元素内的文本行数的详细内容,更多请关注双恒网络其它相关文章!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » 如何计算DOM元素内的文本行数
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?