简体   繁体   English

如何使用javascript在html中获取元素背景图像

[英]How to get the element background image in html using javascript

I want to get all the html page elements' background images that are set using css or the element background property. 我想获得使用css或元素背景属性设置的所有html页面元素的背景图像。

how can I do this using javascript? 我怎么能用javascript做到这一点?

You could us jquery: 你可以我们jquery:

imgs = [];
$("*").each(function(i) { 
  if($(this).css("background-image") != "none") { 
    imgs.push($(this).css("background-image")); 
  } 
});

The getStyle() function below was taken from http://www.quirksmode.org/dom/getstyles.html#link7 (and slightly modified). 下面的getStyle()函数取自http://www.quirksmode.org/dom/getstyles.html#link7 (稍作修改)。

Of course you need to make sure the DOM is ready. 当然,您需要确保DOM已准备就绪。 An easy way to do that is to place the script toward the bottom of the page, just inside the closing </body> tag. 一种简单的方法是将脚本放在页面底部,就在结束</body>标记内。

<script type="text/javascript">
    function getStyle(x, styleProp) {
        if (x.currentStyle) var y = x.currentStyle[styleProp];
        else if (window.getComputedStyle) var y = document.defaultView.getComputedStyle(x, null).getPropertyValue(styleProp);
        return y;
    }

       // Get all elements on the page
    var elements = document.getElementsByTagName('*');

       // store the results
    var results = [],
        i = 0,
        bgIm;

       // iterate over the elements
    for (;elements[i];i++) {
             // get the background-image style property
        bgIm = getStyle(elements[i], 'background-image');

             // if one was found, push it into the array
        if (bgIm && bgIm !== 'none') {
            results.push(bgIm);
        }
    }

       // view the console to see the result
    console.log(results);
</script>

It sounded like you want the path to the images themselves. 这听起来像你想要自己的图像路径。

If you wanted the actual elements, change: 如果您想要实际元素,请更改:

results.push(bgIm);

to: 至:

results.push(elements[i]);

暂无
暂无

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

相关问题 如何使用 JavaScript 获取元素的背景图片 URL? - How to get background image URL of an element using JavaScript? 如何使用JavaScript从此html中获取input元素的值,并更改input元素的背景 - How to get value from input element from this html using JavaScript and also changing the background of the input element 如何在不绑定的情况下获取容器元素的 HTML 标记并使用 VueJS 更改其背景图像? - How to get an HTML tag of a container element without binding and change it background image Using VueJS? 使用Jquery或Javascript获取HTML中div的背景图像的尺寸 - Get Dimensions of background Image of div in HTML using Jquery or Javascript 如何使用JavaScript获取HTML元素的属性 - How to get attribute of HTML element using JavaScript 无法使Canvas元素中的图像显示为DIV背景图像(HTML / Javascript)? - Unable to get image in Canvas element to appear as DIV background image (HTML/Javascript)? 如何在 JavaScript 中使用 css 属性设置 HTML 元素的背景颜色 - How to set background color of HTML element using css properties in JavaScript 如何使用CSS动画/ JavaScript使具有背景图像的元素出现 - How to make an element with background image appear using css animation/javascript Javascript获取元素背景图片,但消除“ url()” - Javascript get element background image BUT eliminate the “url()” 如何使用JavaScript函数在HTML页面中正确设置背景图片 - How to correctly set background image in a HTML page using a JavaScript function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM