javascript - 如何选择具有相同锚文本和href值的链接
问题描述
我想对锚点与 href 属性值相同的链接设置不同的样式:
<a href="https://google.com">https://google.com</a>
是否可以单独使用 CSS 来做到这一点,或者我需要 JavaScript 吗?
解决方案
感谢您评论这在 CSS 中是不可能的。
我最终得到了这个 JavaScript 解决方案:
document.addEventListener("DOMContentLoaded", () => {
for (item of document.querySelectorAll("a[href*='//']")) {
if (item.getAttribute('href') == item.innerHTML) {
//item is an HTML link with anchor same as innerHTML.
//in this case, I just add a class to it
item.classList.add('no-after-content')
}
}
})
推荐阅读
- python - 计算数据框中列内的列中的每个值
- c# - 如何正确使用 HttpClient 类与代理和 cookie 更改 PER 请求(使用多线程)
- hyperledger-fabric - 启动结构服务器时 ./startFabric.s 显示错误
- docker - 在 docker-compose 文件中使用 buildx build linux/arm64
- angular - 如何在 Angular 应用程序中取消订阅 http post 请求?
- c# - 是否可以让 gRPC 服务器也充当客户端?C#
- python - 如果新行以“|”开头,则将行与上一行合并 数据框中的管道
- powershell - Powershell 解压缩扩展名为 .zip 的文件并将其移动到另一个文件夹
- javascript - Javascript在Excel中将两列名称合并为一列
- c++ - 对 std::vector 的 SINGLE BIT 的按位运算
. 为什么会出错?