知乎贺老live

本文介绍了一种利用JavaScript获取网页上所有标签的方法,并演示了如何使用一行代码实现数组去重。此外,还讨论了如何为已访问的链接添加视觉标记。

1 leetcode 考题

2 mdn 学习网站

 

练习 1 获取一个网页全部的标签

① document.getElementsByTagName('*')获取全部的nodeList,然后遍历并用一个对象保存(localName,或者tagName属性)

② document.querySelectorAll('*')获取全部的nodeList,然后遍历并用一个对象保存

③ 浏览器自带$,通过$$('*')也可达到统一的效果

现在我们可以用一行代码实现数组去重了

let array = Array.from(new Set([1, 1, 1, 2, 3, 2, 4]));

let nodeList, arrList, targetList;
[...nodeList] = document.getElementsByTagName('*');

arrList = nodeList.map( item => {
   return item.localName; 
})

targetList = Array.from(new Set(arrList));

console.log(targetList)

 

2 为访问的链接添加一个已读的标签

visited 只能操作和颜色有关的属性了

color, background-color, border-color, outline-color

 

转载于:https://www.cnblogs.com/luguiqing/p/8125515.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值