如何准确靶向将其源存储在CSS中的图像元素?
我正在使用Vite将图像源存储在CSS中的位置。在一个功能中,我必须获得图像的来源,但我不确定如何从CSS中提取它。以下是我选择IMG标签以及如何存储IMG源的方式。我如何在JavaScript中写下此内容以将CSS内容放入数组中?
// JS
this.images = [...document.querySelectorAll('img')];
//css
#firstimage {
content: url(./assets/img1.jpg);
}
#secondimage {
content: url(./assets/img1.jpg);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OP需要代码> 查询图像 - 仅
Nodelist
的每个图像 - 元素(将其按其计算的content> content> style> style> style> style> style> style> style> style> style> style> style> style> style> style> 。
编辑 ...由于上述(初始)答案错过了OP要求的一个(最重要)。
首先提供的解决方案需要从
filter
更改为降低
任务,其中一个(对于前者)不仅是content
通过简单的正则值,例如...^url \( 。+\)$
...但(如后者) /a> forgex的图像源,例如...
^url \((?&lt) ; >命名捕获组
。
The OP needs to
filter
each image-element of the queried images-onlyNodeList
(after casting it into an array type) by its computedcontent
style-property with the help ofwindow.getComputedStyle
.Edit ... due to the fact that the above (initial) answer missed one (the most important) of the OP's requirements.
The firstly provided solution then needs to be changed from a
filter
to areduce
task where one (as for the former) not just doestest
thecontent
value by a simple regex like ...^url\(.+\)$
... but also (as for the latter) captures the image-source by a regex like ...^url\((?<src>.+)\)$
... which features a named capturing group.