javascript - 如何给被文本包围的链接加下划线?

标签 javascript html css

我需要在悬停时被文本包围的 anchor 链接下划线。

我正在寻找类似于下面的结果,但没有定位来自 id 或类的任何链接,因为它对所有链接都是相同的。

如何使用 css(如果没有,则使用 javascript)为被文本包围的链接添加下划线 可能吗?

a{
  text-decoration: none;
}

a#withText:hover{
 text-decoration: underline; 
}
<span> <a href="#"> Alone link </a> <span>
<br/>

<span> Text around <a id="withText" href="#"> Text surrounded link </a> Text around </span>

最佳答案

您可以使用 span > a:hover 这将定位作为 span 的直接子级的所有 a

a{
  text-decoration: none;
}

span > a:hover{
 text-decoration: underline; 
}
<a href="#"> Alone link </a>
<br/>

<span> Text around <a id="withText" href="#"> Text surrounded link </a> Text around </span>

关于javascript - 如何给被文本包围的链接加下划线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47807345/

相关文章:

javascript - react : How do I use my consumer inside ComponentDidMount to change state?

javascript - 如何使用 CodeMirror 在 TextArea 中添加值

html - 调整浏览器大小时将子 div 文本换行到下一行

css - 包装器 div 高度为 0,内部有 float 元素

javascript - 即使外部页面发生错误也执行 javascript

javascript - 如何在书写时将表情符号字符串转换为其图标?

javascript - 格式化无序列表,使其不像段落那样缩进

javascript - 如何将覆盖图标放在文本框/文本区域中

php - 如何让我的网站自动缩小到 90%

html - 只用CSS实现一个简单的下拉菜单