gpt4 book ai didi

javascript - 如何选择具有相同 anchor 文本和 href 值的链接

转载 作者:太空宇宙 更新时间:2023-11-04 07:23:09 24 4
gpt4 key购买 nike

我想为 anchor 与 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')
}
}
})

关于javascript - 如何选择具有相同 anchor 文本和 href 值的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50116184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com