简体   繁体   English

querySelector为h1和p + p元素选择最接近的父级-可能吗?

[英]querySelector to select the closest parent for h1 and p+p elements - is it possible?

EDIT The structure is unknown - it should work for any news site, where there is a container/wrapper, that contains h1 and p+p (everything related to the main article of that page) 编辑结构未知-它应适用于任何包含h1和p + p(与该页面的主要文章相关的内容)的容器/包装程序的新闻网站

What I want can also be achieved with 我想要的也可以用

let story = document.querySelector('p+p').parentElement;
while(!story.querySelector('h1')){
    story = story.parentElement;
}
console.log(story);

But I'm wondersing if there's a better solution? 但是我想知道是否有更好的解决方案?

Every news article has h1 tag and paragraphs with text. 每个新闻文章都有h1标签和带有文字的段落。 I'm wondering if it's possible to write a querySelector that will select the closest parent/container element that contains both h1 and p+p . 我想知道是否有可能编写一个querySelector来选择包含h1和p + p的最近的parent / container元素 The trick is that it should work with any news website... 诀窍是它可以与任何新闻网站一起使用...


when I write 当我写

document.body.querySelector('* h1')

I can get h1 container, but there's might be multiple h1 tags on the page according to HTML5 我可以获得h1容器,但是根据HTML5,页面上可能有多个h1标签

I know about querySelectorAll but this will not help in my case 我知道querySelectorAll,但这对我而言无济于事


when I write 当我写

document.body.querySelector('* p+p').parentElement

I can get container for article body with paragraphs of text 我可以获取带有文本段落的文章正文的容器


My question is if it's possible to combine those two examples into a single query? 我的问题是,是否可以将这两个示例合并为一个查询? (w/o loop and multiple querys) Something like... (不带循环和多个查询)类似...

document.body.querySelector('* ~ h1 ~ p+p')
document.body.querySelector('* h1 p+p')

...but workable 😅 ...但可行😅

I created a jQuery solution. 我创建了一个jQuery解决方案。

Stack Snippet 堆栈片段

 $('.block').each(function(e) { if ($(this).find('h4').length > 0 && $(this).find('p+p').length > 0) { console.log('block ' + (e + 1)) } }); 
 body { font: 13px Verdana; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="block vv"> <h4>heading</h4> <p>para1</p> <p>para2</p> </div> <div class="block"> <h4>heading</h4> <p>para1</p> </div> <div class="block"> <h4>heading</h4> <p>para1</p> <p>para2</p> </div> 

I hope this will help you! 我希望这能帮到您!

My question is if it's possible to combine those two examples into a single query? 我的问题是,是否可以将这两个示例合并为一个查询?

tl;dr: No, it's not. tl; dr:不,不是。 Certainly not today, and probably not anytime soon. 当然不是今天,而且可能不会很快。 If you're not interested in anything beyond a practical answer, this is all you need to know; 如果您对实际答案以外的内容不感兴趣,这就是您需要知道的全部内容; everything past this paragraph is background information pertaining to a feature that doesn't exist in the standard yet. 本段之后的所有内容都是与标准中尚不存在的功能有关的背景信息。


It depends on what you mean by "a single query". 这取决于您所说的“单个查询”。 Do you mean "a single assignment"? 您是说“单项作业”吗? If so, the :has() pseudo-class from jQuery , slated for standardization in Selectors 4 , will help tremendously: 如果是这样, jQuery:has()伪类(将在Selectors 4中进行标准化)将极大地帮助您:

// In jQuery: $('p+p').closest(':has(h1)')
let story = document.querySelector('p+p').closest(':has(h1)');

But notice that it's used in conjunction with Element#closest() , and furthermore, :has() is not supported natively yet, and there are no plans to implement it in any browser in the near future. 但是请注意,它与Element#closest()结合使用,此外, :has()本身还不受支持,并且没有计划在不久的将来在任何浏览器中实现它。 So if jQuery is not an option, what you have is the best you've got for a long while. 因此,如果没有jQuery,那么您拥有的将是您长期以来所拥有的最好的东西。

If by "a single query" you mean "a single selector", then the answer is no, because it's not possible to write a selector matching the closest common ancestor of two or more descendants. 如果通过“单个查询”表示“单个选择器”,那么答案是否定的,因为不可能编写与两个或多个后代的最接近公共祖先匹配的选择器。 The closest would be :has(h1):has(p+p) , but that will match every such ancestor, starting with the innermost of these and leading all the way up to the root element. 最接近的是:has(h1):has(p+p) ,但是它将匹配每个这样的祖先,从它们的最内层开始,一直到根元素。 There is no selector for matching just the innermost of these. 没有选择器仅匹配其中的最内层。

Based on the website you provided just use a child and sibling selector. 根据您提供的网站,只需使用子级和同级选择器。 The following will work.: 以下将起作用:

window.document.querySelector("*>h1, * p+p").parentNode

It selects all elements with an h1 and all elements with multiple p tags. 它选择所有带有h1的元素和所有带有多个p标签的元素。 I tried it onthe page you provided and it produced the same result as your JS code. 我在您提供的页面上进行了尝试,并产生了与JS代码相同的结果。

A few additional notes, since you didn't specify jquery I gave you a JavaScript solution. 另外一些注意事项,因为您未指定jquery,所以我为您提供了JavaScript解决方案。 Currently there is no parent selector or querySelector so parentNode is the closest thing to that. 当前没有父选择器或querySelector,因此parentNode是最接近的选择器。 Lastly, your original solution specified article (and not div like the page has). 最后,您的原始解决方案指定了文章(而不是该页面的div)。 If you want that, just swap out div for article. 如果需要,只需将div换成文章即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM