问题
The structure of my HTML is like so
<div>
<div>
<h1>Something</h1>
</div>
<div>
<h1 class='Handle'>Something</h1>
</div>
</div>
In the event that the div > div
does not have a child with the class "Handle" I want the the div > div
to have the style cursor:move;
. How would I go about doing this in pure CSS, is it even possible?
回答1:
This would work, except no browser currently supports it as far as I am aware. jQuery does though, however.
div > div:not(:has(h1.Handle)) {
cursor: move;
}
回答2:
There is no parent selector in CSS, so what you are asking is not possible. What you can do is put the cursor:move
on every h1 that doesnt has the class "Handle" by using the attribute selector.
h1:not([class=Handle]) {
cursor:move;
}
http://jsfiddle.net/4HLGF/
Another option is to adjust your HTML, and move your h1 on the same level as the div.
<div>
<h1>Something</h1>
<div>
dragable content
</div>
<h1 class='Handle'>Something</h1>
<div>
non dragable content
</div>
</div>
Now you can do the same check on the h1, and target the div that comes after it.
h1:not([class=Handle]) + div {
cursor:move;
}
http://jsfiddle.net/4HLGF/2/
回答3:
Try
div > div h1:not([class=Handle]) {
cursor:move;
}
来源:https://stackoverflow.com/questions/18189147/selecting-an-element-that-doesnt-have-a-child-with-a-certain-class