gpt4 book ai didi

javascript - 如何计算javascript html字符串变量中的行数

转载 作者:行者123 更新时间:2023-11-28 15:16:46 24 4
gpt4 key购买 nike

我有带有 html 字符串数据的 JavaScript 变量,如下格式

var html_string = '<p class="MsoListParagraph" style="margin-left:28.5pt;mso-add-space:auto;text-indent:-28.5pt;mso-list:l0 level2 lfo1;tab-stops:list 28.5pt"><!--[if !supportLists]--><b><span lang="EN-US">1.1<span style="font-weight: normal; font-stretch: normal; font-size: 7pt; line-height:normal; font-family: Times New Roman;"></span></span></b><!--[endif]--><b><span lang="EN-US">Purpose of the Document<o:p></o:p></span></b></p><p class="MsoNormal" style="margin-left:28.5pt;tab-stops:80.25pt"><span lang="EN-US">This document helps to understand the design aspects of the ASSMail web based project. This document details the technical specification for the project.</span></p>';

我尝试根据break语句进行计数,但它不起作用。

我想计算浏览器 View 中此 html 字符串中找到的行数。有人请帮忙如何完成这一过程吗?

最佳答案

根据您的后续评论,您可以使用 DOMParser 解析内容,并由此了解段落数:

var parser = new DOMParser(),
doc = parser.parseFromString(html_string, "text/html"),
paragraphs = doc.querySelectorAll('p').length;

alert('there is '+paragraphs+' paragraphs');

演示 -> http://jsfiddle.net/nfwy006u/1/

关于javascript - 如何计算javascript html字符串变量中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33548340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com