【问题标题】:How to count number of divs in a td如何计算 td 中的 div 数
【发布时间】:2022-10-13 23:15:50
【问题描述】:

我想计算一个表的 td 中的 div 数并将其存储在一个变量中。

html 图片:https://imgur.com/a/T476Tkx

或者这里是 html 的示例:

<td colspan="1" class="timeslot" xpath="1">

<div class="timeslot" style="height: 133px; background-color: rgb(167, 211, 255); width: 145px;"><div class="timenotitle"><span> 7:00 am</span></div></div>

<div class="timeslot" style="height: 133px; background-color: rgb(167, 211, 255); width: 145px;"><div class="timenotitle"><span> 8:00 am</span></div></div>

<div class="timeslot" style="height: 133px; background-color: rgb(167, 211, 255); width: 145px;"><div class="timenotitle"><span> 9:00 am</span></div></div>

</td>

我怎样才能做到这一点?

【问题讨论】:

标签: html variables html-table count


【解决方案1】:

尝试这个 :

const arr = document.getElementsByTagName('td')[0].children;
const totalDivCount = Object.values(arr).filter(d => d.nodeName == "DIV").length

totalDivCount 保存 td 元素中存在的div 数量的值。

您还可以将ID 添加到 td 并使用它轻松获取元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 2021-07-13
    • 1970-01-01
    相关资源
    最近更新 更多