[Javascript] Deep Search nested tag element in DOM tree
JavaScript in Tree dom Element search Deep TAG
2023-09-14 08:59:17 时间
// For example you want to search for nested ul and ol in a DOM tree branch
// Give example <ol> <li> <ol> <li></li> </ol> </li> </ol> should retrun 2
function solution( tags = ['ul', 'ol']) {
const [uls, ols] = tags.map(tag => Array.from($(`${tag}`)));
const [logUl, logOl] = tags.map(tag => new Logger(`${tag}`));
deepSearch(uls, 'ul', logUl);
deepSearch(ols, 'ol', logOl);
return logUl.count + logOl.count;
}
class Logger { constructor(tag) { this.tag = tag; this.num = 0; } get count () { return this.num; } get tagName () { return this.tag; } countOne() { this.num++; } } function deepSearch(els = [], tag = "", log) { // if no such elements passed in if (!els.length) { return; } log.countOne(); // loop though the els and check whether contains tag els.forEach(el => { const targets = Array.from(el.getElementsByTagName(`${tag}`)); if (targets.length) { deepSearch(targets, tag, log); } }); }
相关文章
- 【说站】javascript算法的复杂度介绍
- JavaScript——作用域
- 内存数据库 mysql-mysql in memory_In-Memory:内存数据库
- javascript【写法规范】
- 检测设备平台,操作系统,方向 Javascript 库:Device.js
- python压缩javascript文件代码详解编程语言
- 表★★ MySQL索引优化:IN表的应用(mysql索引in)
- 语句SQL Server中使用IN语句处理多值查询(sqlserver中in)
- MySQL使用IN查询时数量超限问题的解决方法(mysql中in超过数量)
- MySQL中使用IN子句出现重复值问题的解决方法(mysql中in有重复值)
- MYSQL中IN关键字如何高效处理大量数据(mysql中in关键字)
- 利用Oracle中的IN子句改善查询效率(oracle中的in子句)
- 在Oracle数据库中优化IN子句(oracle中in优化)
- 研究Oracle IN语句的技巧与窍门(oracle in的语句)
- FF和IE之间7个JavaScript的差异
- Javascript阻止javascript事件冒泡,获取控件ID值
- 你不一定知道的关于JavaScript的正则表达式
- 百度手写板代码JavaScript远程调用的实现(鼠标输入法)
- JavaScript和CSS交互的方法汇总
- 关于Javascript加载执行优化的研究报告