html - 设置除包含图像的 anchor 之外的所有 anchor 的样式,而不向图像标签添加 class 或 id?

标签 html css

在下面的示例中,我需要向文本 anchor 添加填充和背景颜色属性。然后,我需要从包含图像的 anchor 中排除填充和背景颜色。

<p>
  <a href="#">this is a text link that needs to be styled</a>
  <a href="#"><img src="image/name.jpg" alt="this needs to be excluded from styling" /></a>
</p>

如果我的文本链接上有红色背景和填充,我不希望链接图像上出现相同的红色背景和填充。图片将始终位于自己的 anchor 中,不会与同一 anchor 中的文本混合。

问题是我无法将类或 ID 添加到 img 标签 - 我没有对该数据的编辑控制权。

那么如何为所有 anchor 添加 CSS 属性,同时排除包含图像的 anchor ?

最佳答案

使用 JQuery:

$(function() {
    $('a:not(:has(img))').css('background','red');
});

关于html - 设置除包含图像的 anchor 之外的所有 anchor 的样式,而不向图像标签添加 class 或 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2404170/

相关文章:

html - 每 2 行后制作一个 html 表格的边框

html - iPad 上的 iframe 大小

html - 一个关于溢出 :hidden 的简单 css 问题

javascript - 如何让用户选择的背景图像持久

html - 为什么我不能居中我的选项卡?

html - 当 div 使用自动定位时,如何将 div 与其上方的 div 边缘对齐?

javascript - wordpress 谷歌地图调整大小

javascript - 对象在与其他对象发生碰撞之前不会被移除

html - 双括号未在 Angular.js html 中注册

javascript - 复制网站,包括图像和样式