0

0

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

王林

王林

发布时间:2023-08-20 17:49:43

|

1376人浏览过

|

来源于tutorialspoint

转载

如何计算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中存在的总行数。



   Count the text lines inside of DOM element


   
Welcome to tutorialspoint
Visit us at: https://www.tutorialspoint.com/
Buy the course of your domain
Get the certificate
Thank you for visiting
Total number of lines:

在下面的图像中,它显示了上面示例的输出。其中包含了“30px”的行高,它将DOM元素的“offsetHeight”分割,并返回span标签中元素的数量。

Algorithm

步骤 1  创建一个HTML模板,在其中添加一个div元素。创建一个span标签,用于生成输出。

TapNow
TapNow

新一代AI视觉创作引擎

下载

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 tutorialspoint
Visit us at: https://www.tutorialspoint.com/
Thank you for visiting
Total number of lines:

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.

相关专题

更多
html版权符号
html版权符号

html版权符号是“©”,可以在html源文件中直接输入或者从word中复制粘贴过来,php中文网还为大家带来html的相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

611

2023.06.14

html在线编辑器
html在线编辑器

html在线编辑器是用于在线编辑的工具,编辑的内容是基于HTML的文档。它经常被应用于留言板留言、论坛发贴、Blog编写日志或等需要用户输入普通HTML的地方,是Web应用的常用模块之一。php中文网为大家带来了html在线编辑器的相关教程、以及相关文章等内容,供大家免费下载使用。

648

2023.06.21

html网页制作
html网页制作

html网页制作是指使用超文本标记语言来设计和创建网页的过程,html是一种标记语言,它使用标记来描述文档结构和语义,并定义了网页中的各种元素和内容的呈现方式。本专题为大家提供html网页制作的相关的文章、下载、课程内容,供大家免费下载体验。

467

2023.07.31

html空格
html空格

html空格是一种用于在网页中添加间隔和对齐文本的特殊字符,被用于在网页中插入额外的空间,以改变元素之间的排列和对齐方式。本专题为大家提供html空格的相关的文章、下载、课程内容,供大家免费下载体验。

245

2023.08.01

html是什么
html是什么

HTML是一种标准标记语言,用于创建和呈现网页的结构和内容,是互联网发展的基石,为网页开发提供了丰富的功能和灵活性。本专题为大家提供html相关的各种文章、以及下载和课程。

2891

2023.08.11

html字体大小怎么设置
html字体大小怎么设置

在网页设计中,字体大小的选择是至关重要的。合理的字体大小不仅可以提升网页的可读性,还能够影响用户对网页整体布局的感知。php中文网将介绍一些常用的方法和技巧,帮助您在HTML中设置合适的字体大小。

505

2023.08.11

html转txt
html转txt

html转txt的方法有使用文本编辑器、使用在线转换工具和使用Python编程。本专题为大家提供html转txt相关的文章、下载、课程内容,供大家免费下载体验。

311

2023.08.31

html文本框代码怎么写
html文本框代码怎么写

html文本框代码:1、单行文本框【<input type="text" style="height:..;width:..;" />】;2、多行文本框【textarea style=";height:;"></textare】。

423

2023.09.01

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

9

2026.01.16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 4.6万人学习

PostgreSQL 教程
PostgreSQL 教程

共48课时 | 7.3万人学习

Excel 教程
Excel 教程

共162课时 | 12万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号