As you are open to JavaScript solution, here's what I've done.
var elms = document.getElementsByTagName('h1');
for(var i = 0; i < elms.length; i++) {
var elm = elms[i];
if(!(elm.getElementsByTagName('span').length > 0))
elm.className = "select-next-table";
}
.select-next-table + table {
border: 1px solid red;
}
table {
width: 100px;
height: 100px;
border: 1px solid #000;
}
<h1><span>Don't select the next table</span></h1>
<table></table>
<h1>Select the next table</h1>
<table></table>
What am doing here is fetching all h1
tags and looping them. Later, I try to search span
inside the h1
tag, if I don't get any, then I append a class
to the h1
element, and later, I use .select-next-table + table
to select the tables which are adjacent to h1
tags, and the h1
doesn't have any span
tags.
You can definitely select the table which is rendered after h1
using
h1 + table
But you cannot check whether the h1
contains a span
element because once you go inside the h1
tag, you cannot select the parent again. Hence, for such things, you might have to use JavaScript.