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 array, which will return the number of text lines arrayable .
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中文網其他相關文章!

htmltagsareessentialforwebdevelopmentastheyandendenhancewebpages.1)semantictagsimproveaccessibilityandseo.2)semanteLayOut,語義和互動性。 3)poseriblesibilityandseoandseo.3)poseriblesoftagscanoftagscanoftagscanoptagscanoptimizeperefeneandimizeanDenSuroceRecRoscRoss-BrowserCrowserCercerComercompatibility。

一致的HTML編碼風格很重要,因為它提高了代碼的可讀性、可維護性和效率。 1)使用小寫標籤和屬性,2)保持一致的縮進,3)選擇並堅持使用單引號或雙引號,4)避免在項目中混合使用不同風格,5)利用自動化工具如Prettier或ESLint來確保風格的一致性。

在Bootstrap4中實現多項目輪播的解決方案在Bootstrap4中實現多項目輪播並不是一件簡單的事情。雖然Bootstrap...

如何實現鼠標滾動事件穿透效果?在我們瀏覽網頁時,經常會遇到一些特別的交互設計。比如在deepseek官網上,�...

無法直接通過CSS修改HTML視頻的默認播放控件樣式。 1.使用JavaScript創建自定義控件。 2.通過CSS美化這些控件。 3.考慮兼容性、用戶體驗和性能,使用庫如Video.js或Plyr可簡化過程。

在手機上使用原生select的潛在問題在開發移動端應用時,我們常常會遇到選擇框的需求。通常情況下,開發者傾...

在手機上使用原生select的弊端是什麼?在移動設備上開發應用時,選擇合適的UI組件是非常重要的。許多開發者�...

使用Three.js和Octree優化房間內第三人稱漫遊的碰撞處理在Three.js中使用Octree實現房間內的第三人稱漫遊並添加碰�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

記事本++7.3.1
好用且免費的程式碼編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。