如何计算DOM元素内的文本行数
Overview
The number of the text lines inside the Document Object Model (DOM) element whether it would be any element that is
or
在第二种方法中,我们将使用数组的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.
Count the text lines inside of DOM element Welcome to tutorialspointTotal number of lines: <script> const textLines = document.getElementById("lines").innerText; const numLines = textLines.split("").length; document.getElementById("out").innerText=numLines-1; </script>
Visit us at: https://www.tutorialspoint.com/
Thank you for visiting
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.
结论
在第一个示例中,我们必须使用 parseInt() 解析行高的值,因为它也包含字符串值,如果我们将该值除以“offsetHeight”值,那么它将返回 (NaN),这意味着“不是一个数字”。所以我们使用了parseInt(),但是如果我们想以十进制形式返回它,我们也可以使用parseFloat()。
以上是如何计算DOM元素内的文本行数的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

本文讨论了HTML&lt; Progress&gt;元素,其目的,样式和与&lt; meter&gt;元素。主要重点是使用&lt; progress&gt;为了完成任务和LT;仪表&gt;对于stati

本文讨论了html&lt; datalist&gt;元素,通过提供自动完整建议,改善用户体验并减少错误来增强表格。Character计数:159

本文讨论了使用HTML5表单验证属性,例如必需的,图案,最小,最大和长度限制,以直接在浏览器中验证用户输入。

本文讨论了HTML&lt; meter&gt;元素,用于在一个范围内显示标量或分数值及其在Web开发中的常见应用。它区分了&lt; meter&gt;从&lt; progress&gt;和前

本文讨论了视口元标签,这对于移动设备上的响应式Web设计至关重要。它解释了如何正确使用确保最佳的内容缩放和用户交互,而滥用可能会导致设计和可访问性问题。

本文讨论了&lt; iframe&gt;将外部内容嵌入网页,其常见用途,安全风险以及诸如对象标签和API等替代方案的目的。

HTML适合初学者学习,因为它简单易学且能快速看到成果。1)HTML的学习曲线平缓,易于上手。2)只需掌握基本标签即可开始创建网页。3)灵活性高,可与CSS和JavaScript结合使用。4)丰富的学习资源和现代工具支持学习过程。
