如何计算DOM元素内的文本行数

王林
王林 转载
2023-08-20 17:49:43 764浏览

如何计算DOM元素内的文本行数

Overview

The number of the text lines inside the Document Object Model (DOM) element whether it would be any element that is <div>, <p> or <label> 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元素内的文本行数的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除