您拥有的选择器几乎是正确的。
a#followed_by+h4.li_group ~ div.soda a
的~
作品不同,以在其将选择选择器的第一部分之后的任何匹配元件的+
,而+
将仅选择紧接在选择器的第一部分以下元件。 当然,“第一部分”我指的是a#followed_by+h4.li_group
。
我也改变了选择器找到div.soda
而不是div.odd
,所以你得到所有相关的元素,而不是只是奇数。
因为这样的CSS选择器的工作,我们不能问“仅元素,直到edited_into
”。但是,我们可以使用JavaScript解决此问题。
简单的for
循环与条件break
将是最简单的方法。
var titles = [];
var items = document.querySelectorAll('a#followed_by+h4.li_group ~ div.soda a');
//firstEditedIntoItem represents the first element in
//the `edited_into` section. If it's null, then there
//is none, and we have nothing to worry about.
var firstEditedIntoItem =
document.querySelector
('a#edited_into+h4.li_group+div.soda a, a#spin_off_from+h4.li_group+div.soda a');
// Note: This selector will find the first instance that matches any of the
// specified sections. You could add another and replace `spin_off_from` with
// another section id.
for(var i=0; i<items.length; i++) {
var it = items[i];
//don't accept elements after 'edited_into'
// If firstEditedIntoItem is null, it will not match either.
if(it==firstEditedIntoItem) break;
titles.push(it.textContent);
}
console.info(titles.join('\n'));
感谢您的解决方案。使用你的选择器,我得到了“后面跟着”标题的所有名字,这部分是我想要的。不过,我的意图是从标题开始,然后一找到“edited_into”标题就停下来。否则,它会抓住所有人。也许你没有注意到我在上面的描述中也提到了它。再次感谢迈克。 – SIM
哦,当然 - 你是对的我错过了那部分,不知何故。单纯用CSS来做这件事是不可能的 - 除非我错过了一些东西。假设JS没问题,在遇到“edited_into”部分之后的第一个元素后,可以停止处理列表。如果您有兴趣,我可以用一些示例代码更新答案。 – mike
非常感兴趣。顺便说一句,我接受你的答案。希望你会编辑你提供的部分。 – SIM