如果孩子是img,则更改a:after样式
Change a:after style if child is img
我通过应用带有:after伪属性的内容来样式化悬停锚,它在下面添加了一个边框:
a:hover {
position: relative; }
a:hover:after {
content: '';
position: absolute;
left: 0;
display: inline-block;
height: 1em;
width: 100%;
border-bottom: 1px solid #a5cf4c;
margin-top: 0.5em; }
这工作得很好,但也有一些锚周围的图像,我不希望在他们下面的边界。对于CSS,这只适用于不存在的父选择器a:after < img
。我试着用jQuery解决它,但是
不能操作:after,因为从技术上讲,它不是DOM的一部分,因此任何JavaScript都无法访问。参见访问css ":after"
我看了一些没有父母障碍的解决方案,但我不能得到任何地方。有人知道吗?
您可以添加after
与一个类。
$('a:not(:has(img))').addClass('after-affect');
a:hover {
position: relative;
}
a.after-affect:hover:after {
content: '';
position: absolute;
left: 0;
display: inline-block;
height: 1em;
width: 100%;
border-bottom: 1px solid #a5cf4c;
margin-top: 0.5em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Anchor with text -->
<a href="#">test</a>
<!-- Anchor with image -->
<a href="#">
<img src="http://blog.grio.com/wp-content/uploads/2012/09/stackoverflow.png"/>
</a>
相关文章:
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 将样式表插入iframe
- 跟踪在页面加载时应用内联样式的JavaScript
- 如果文本字段为空,则使用JavaScript应用CSS样式
- 定义完全独立的样式信息
- W3C循环样式的JavaScript
- jQuery/JavaScript在线公文包表单-打印样式表
- 使用递归、Ramda.js和无点样式重构字符串的getPermutations()
- missing ) after argument list DATEDIF
- FF和Chromium中CSS样式按钮的外观差异
- 如何首先设置样式<td>表中包含在窗体中的元素
- javascript问题正文样式
- 使用具有内联样式的tidy-html5
- DataTables-创建自定义分页样式(加载更多样式)
- 具有所有样式的文本正在复制到可编辑文本区域
- 如何提供在javascript中编写对对象的重复访问以设置元素样式的简写
- 绑定Range输入以修改样式
- 将 css 样式应用于 javascript 调用中的 :after 伪元素
- 不能为:before,:after编辑css样式
- 如果孩子是img,则更改a:after样式