简体   繁体   English

是否可以使用CSS选择器选择元素的属性?

[英]Is it possible to select an element's attribute with a CSS selector?

I'm looking for a way to use a pure CSS selector (not script) to select an element's attribute, not the element itself. 我正在寻找一种使用纯CSS选择器(而非脚本)来选择元素的属性而不是元素本身的方法。 I know XPath can do it but can a CSS selector? 我知道XPath可以做到,但是CSS选择器可以吗?

Example, given: 示例,给出:

<img alt="image" src="photo.jpg">

Can I get to the src attribute with a CSS selector? 我可以使用CSS选择器进入src属性吗?

Update: 更新:

I don't want to set any element's values, I just want to select the text "photo.jpg". 我不想设置任何元素的值,我只想选择文本“ photo.jpg”。

Because CSS selectors originated as a fundamental part of CSS, and CSS can only apply styles to elements (since attributes are just element metadata, not standalone objects), CSS selectors cannot match attributes alone within CSS. 因为CSS选择器起源于CSS的基本部分,并且CSS只能将样式应用于元素(因为属性只是元素元数据,而不是独立的对象),所以CSS选择器不能单独匹配CSS中的属性。

But I suspect you're not actually asking about CSS here. 但是我怀疑您实际上不是在这里询问CSS。 You're asking about selectors alone. 您仅在询问选择器。 You're probably using a web automation tool such as Selenium or one of the numerous HTML parsing libraries out there that support either CSS selectors or XPath. 您可能正在使用诸如Selenium之类的Web自动化工具,或者使用众多支持CSS选择器或XPath的HTML解析库之一。 Some of these libraries support non-element selectors in the form of pseudo-elements such as ::attr() (I don't remember which ones), you haven't mentioned which tool you're using so I can't tell you for sure if you could use it. 其中一些库以伪元素的形式支持非元素选择器,例如::attr() (我不记得是哪个),您没有提到要使用的工具,所以我不知道您确定是否可以使用它。 Note that this is not the same thing as the CSS attr() function mentioned in the comments — that is a CSS function, which is a value, not a selector, and therefore it cannot be used in a selector. 请注意,这不是同样的事情CSS attr()在评论中提到的功能-这是一个CSS功能,这是一个值,而不是一个选择,因此它不能在选择使用。

But if your library doesn't have such a feature then you'll need to either select the img element directly and query its src attribute separately (again, how you do this depends entirely on what you're using, which is why it helps to be specific about this sort of thing), or use XPath if possible. 但是,如果您的库没有此功能,则需要直接选择img元素并分别查询其src属性(同样,如何执行此操作完全取决于您使用的内容,这就是为什么它有帮助的原因(具体说明这类事情),或者尽可能使用XPath。

CSS Tricks has an article that I believe answers your question: https://css-tricks.com/almanac/selectors/a/attribute/ 我认为CSS Tricks有一篇文章可以回答您的问题: https : //css-tricks.com/almanac/selectors/a/attribute/

If you are trying to set the value of a certain element attribute using css, I'm pretty certain that is impossible for anything other than the content property. 如果您尝试使用css设置某个元素属性的值,那么我可以肯定,除了content属性之外,这是不可能的。

CSS is not a programming language and can't process data. CSS不是一种编程语言,无法处理数据。

Its sole purpose it to tell the browser how a certain element should look like, like in coloring a text red. 它的唯一目的是告诉浏览器某个元素的外观,例如将文本涂成红色。

To process data in a web page you use javascript, which can make use of CSS rules though, to grab a certain type of elements in a web page, for example this, which will return a list of all elements of type img 要处理网页中的数据,您可以使用javascript(虽然可以利用CSS规则)来获取网页中某种类型的元素,例如,这将返回img类型的所有元素的列表

var imglist = document.querySelectorAll('img');

Now, having a list you can loop through it and get each src like this 现在,有了列表,您可以遍历列表并像这样获取每个src

Array.prototype.slice.call(document.querySelectorAll("img")).forEach(function(img) {

    var imgsrc = img.src;

    // imgsrc now holds the image url, in your case "photo.jpg"

});

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

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