html - 当两者都在同一 ID 内时,如何仅选择不在链接内的图像?

标签 html css

我有一些无法更改的代码,如下所示:

<div id="wombat">
    <a href=""><img src="a"></a>
    <img src="b">
</div>

我只想定位 img b。 #wombat img {blah} 得到了他们两个。我知道 #wombat a img 只会针对 img a,但有没有办法只针对 img b?

谢谢!

最佳答案

您可以使用兄弟或后代运算符,具体取决于您要实现的目标:

#wombat > img {
    /* any images that reside directly below #wombat, without any more levels in between */
}

a ~ img {
    /* any image that follows as a sibling to an anchor */
}

a + img {
    /* an image that follows as direct sibling to an anchor */
}

参见:

关于html - 当两者都在同一 ID 内时,如何仅选择不在链接内的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19591555/

相关文章:

javascript - 为相同的 <a> 标签设置不同的超链接(使用 AngularJs)

html - 无法使Footer的文本对齐或margin:auto居中。小文字也不起作用

javascript - 使用向上/向下箭头通过 AngularJS 聚焦和选择列表值

jquery - 如何在具有悬停效果时使两行中的 12 个 block 响应

jquery - 将静态 slider 转换为响应式 slider

javascript - 带有 CSS 动画的 SVG 预加载器,在每个页面加载时随机化

html - Rails 和 html - 原始样式的 fontawesome

javascript - 锁定鼠标滚轮滚动到焦点 div

javascript - Twitter Bootstrap 轮播中的交互式 JavaScript 失败

css - 我的 div 在彼此下面堆积