Javascript 如何计算<;br>;元素内的标记

Javascript 如何计算<;br>;元素内的标记,javascript,jquery,html,Javascript,Jquery,Html,假设我有: <div class="words-container"> <div class="words"> here are words<br> here are words<br> here are words<br> here are words<br> </div> </div> 以下是单词 以下是单词 以下是单词 以下是单词 使用Javascr

假设我有:

<div class="words-container">
  <div class="words">
    here are words<br>
    here are words<br>
    here are words<br>
    here are words<br>
  </div>
</div>

以下是单词
以下是单词
以下是单词
以下是单词
使用Javascript或jQuery,我如何计算填充在“words”div中的

标记的确切数量?我需要一个方法知道有多少行

不确定我在这里做什么:

$countWords = $('.words-container')
if ($countWords.length) {
  var hahaha = $('.words').find('<br>').length
  console.log(hahaha.length)
}
$countWords=$('.words容器')
if($countWords.length){
var hahaha=$('.words').find('
').length console.log(哈哈哈.长度) }
请签出此项

<div class="words-container">
  <div class="words">
    here are words<br>
    here are words<br>
    here are words<br>
    here are words<br>
  </div>
</div>
<script>
    $countWords = $('.words-container')
    if ($countWords.length) {
        var hahaha = $('.words').find('br').length
        alert(hahaha)
    }
</script>

以下是单词
以下是单词
以下是单词
以下是单词
$countWords=$(“.words容器”) if($countWords.length){ var hahaha=$('.words').find('br').length 警惕(哈哈哈) }
您可以计算节点文本表示形式中的换行数(通过
innerText
属性找到),或者使用jQuery计算

标记数:

$('.words')。每个(函数(){
//计算换行
常量行=(this.innerText.match(/\n/g)| |“”).length
控制台日志(行);
//或者,数一数标签
log($('br',this).length);
});

以下是单词
以下是单词
以下是单词
以下是单词
像这样:

console.log($('.words container.words br').length)//4

以下是单词
以下是单词
以下是单词
以下是单词
本机方法


document.getElementsByClassName('words')[0].getElementsByTagName('br').length
$('.words').find('br')。
您已经调用了两次
length
。将其从
console.log(hahaha.length)
中删除。投票以键入方式结束我知道您已经按照标题进行了操作,但问题只是说明:
我如何计算“words”div中填充的标签的确切数量?
@rorymcrossan也更新了一种计算标签的方法。这是真的,但我仍然建议从源代码计算值,而不是浏览器解释从源代码创建的文本