List of All Background Images in DOM

前端 未结 7 1112
旧巷少年郎
旧巷少年郎 2020-12-29 12:00

What\'s the best way to find all of the background images on a given page using javascript?

The ideal end result would be an array of all of the url\'s.

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 12:29

    This is a complicated problem. The reason is that the background-image can be applied to the element by using a separate CSS file. This way parsing all the objects in the DOM and checking for their background-image will not work.

    One of the ways I can see is to create a simple HttpHandler which will work on all the type of images. When images are referenced inside the CSS file they are downloaded as a separate entity. This means HttpHandler will be able to capture the type of the image and then execute on it.

    Maybe a server side solution is the best way to go on this one!

提交回复
热议问题