javascript - 如何使用javascript从网页获取所有图像URL?

标签 javascript html jquery css dom

有几种方法可以使用 JavaScript 加载图像 src url,例如使用 document.images 或选择所有 img 标签并获取 src。

但是我无法找到在 css 中使用图像 url 的方法。

例如,如果网页有以下 css 代码,它会加载 bg.png,但我无法使用上面提到的方法获取该 url。

.bg {
  background-image: url('bg.png');
}

有人知道如何在 css 中使用所有这些 url 吗?

最佳答案

Resource Timing API收集有关出站请求的数据,应该保留以 CSS 和内联样式高效收集图像的能力。

尚未对此进行测试,但类似的内容应该可以帮助您入门:

if ( !('performance' in window) ||
                 !('getEntriesByType' in window.performance) ||
                 !(window.performance.getEntriesByType('resource') instanceof Array)
                 ) {
                      alert('unsupported');
         } else {
            window.addEventListener('load', function() {
               var resources = window.performance.getEntriesByType('resource');
               for(var index in resources) {

                  for(var properties in resources[index]) {
                      console.log(properties);
                      console.log(resources[index][properties]);
                  }
                
               }
            });

关于javascript - 如何使用javascript从网页获取所有图像URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65836625/

相关文章:

javascript - Redux如何在ajax完成后打开一个新窗口

javascript - 修改express.js中的res对象

javascript - 使用 jQuery 模拟单击​​多个复选框以在复选框属性更改时发出 AJAX 请求

jQuery 循环我的淡入/淡出

javascript - 根据自动完成值填充下拉列表

javascript - 使用 Angular 设置 Express

javascript - 在 swiper.js 上,添加停止、播放按钮

html - 图片未正确发送到电子邮件中

javascript - XDK中的混合应用程序 - 如何在同一上传中上传表单数据和文件

jquery - Ajax 表单数据文件上传进度百分比