简体   繁体   English

CSS:选择只有一个父匹配属性选择器的元素

[英]CSS: Select elements with only one parent matching attribute selector

I'm a bit of a noob to coding so sorry if this is a dumb question, but I'm trying to write a general purpose scraper for getting some product data using the "schema.org/Product" HTML microdata.如果这是一个愚蠢的问题,我对编码有点菜鸟,很抱歉,但我正在尝试编写一个通用刮刀,用于使用“schema.org/Product”HTML 微数据获取一些产品数据。

However, I came into an issue when testing ( on this page in particular where the name was being set as "Electronics" from the Breadcrumbs schema ) as there were ancestor elements with different itemtypes/schema.但是,我在测试时遇到了一个问题( 在此页面上,特别是在 Breadcrumbs 模式中将名称设置为“电子”的地方),因为存在具有不同项目类型/模式的祖先元素。

I first have this variable declared to check if the page has an element using the Product schema microdata.我首先声明了这个变量来检查页面是否有使用产品架构微数据的元素。

var productMicrodata = document.querySelector('[itemscope][itemtype="https://schema.org/Product"], [itemscope][itemtype="http://schema.org/Product"]');

I then wanted to select for all elements with the itemprop attribute.然后我想选择所有具有 itemprop 属性的元素。 eg例如

productMicrodata.querySelectorAll('[itemprop]');

The issue however is that I want to ignore any elements that have other ancestors with different itemtypes/schema attributes, as in this instance the Breadcrumbs and ListItem schema data is still being included.然而,问题是我想忽略任何具有不同 itemtypes/schema 属性的其他祖先的元素,因为在这种情况下,仍然包含 Breadcrumbs 和 ListItem 架构数据。

I figured I would then just be able to do something like this:我想我可以做这样的事情:

productMicrodata.querySelectorAll(':not([itemscope]) [itemprop]');

However this is still returning matches for the child elements having ancestor elements with different itemscope attributes (eg breadcrumbs).然而,这仍然返回具有具有不同 itemscope 属性(例如面包屑)的祖先元素的子元素的匹配项。

I'm sure I'm just missing something super obvious, but any help on how I can achieve only selecting elements that have only the one ancestor with itemtype="http://schema.org/Product" attribute would be much appreciated.我确定我只是遗漏了一些非常明显的东西,但是对于如何实现仅选择具有itemtype="http://schema.org/Product"属性的只有一个祖先的元素的任何帮助将不胜感激。

EDIT: For clarification of where the element(s) are that I'm trying to avoid matching with are, here's what the DOM looks like on the example page linked.编辑:为了澄清我试图避免与 are 匹配的元素在哪里,这是链接的示例页面上的 DOM 的样子。 I'm trying to ignore the elements that have any ancestors with itemtype attributes.我试图忽略具有 itemtype 属性的任何祖先的元素。

EDIT 2: changed incorrect use of parent to ancestor .编辑 2:将parent错误使用更改为ancestor Apologies, I am still new to this :|抱歉,我还是个新手:|

EDIT 4/SOLUTION: I've found a non-CSS solution for what I'm trying to achieve using the javascript Element.closest() method.编辑 4/解决方案:我找到了一个非 CSS 解决方案,用于我尝试使用 javascript Element.closest()方法实现的目标。 eg例如

 let productMicrodata = document.querySelectorAll('[itemprop]'); let itemProp = {}; for (let i = 0; i < productMicrodata.length; i++) { if (productMicrodata[i].closest('[itemtype]').getAttribute('itemtype') === "http://schema.org/Product" || productMicrodata[i].closest('[itemtype]').getAttribute('itemtype') === "https://schema.org/Product") { itemProp[productMicrodata[i].getAttribute('itemprop')] = productMicrodata[i].textContent; } } console.log(itemProp);

具有 itemtype 父属性的 itemprop 元素

:not([itemscope]) [itemprop] means: :not([itemscope]) [itemprop]表示:

An element with an itemprop attribute and any ancestor with no itemprop ancestor.具有itemprop属性的元素和没有itemprop祖先的任何祖先。

So:所以:

<div>
    <div itemprop>
        <div itemprop> <!-- this one -->
        </div>
    </div>
</div>

… would match because while the parent element has the attribute, the grandparent does not. ... 会匹配,因为虽然父元素具有该属性,但祖父元素没有。

You need to use the child combinator to eliminate elements with matching parent elements:您需要使用子组合器来消除具有匹配元素的元素:

:not([itemscope]) > [itemprop]

[...] help on how I can achieve only selecting elements that have only the itemtype="http://schema.org/Product" attribute would be much appreciated. [...] 关于如何实现只选择只有itemtype="http://schema.org/Product"属性的元素的帮助将不胜感激。

Attribute selectors can take explicit values:属性选择器可以采用显式值:

[myAttribute="myValue"]

So the syntax for this would be:所以这个的语法是:

var productMicrodata.querySelectorAll('[itemtype="http://schema.org/Product"]');

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

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