简体   繁体   English

javascript如何使用style属性查找哪些元素包含背景图像

[英]javascript how do i find which elements contain a background image using the style attribute

I am trying to add script to a web page that is generated as part of a go-daddy web site. 我试图将脚本添加到作为Go-Daddy网站的一部分生成的网页中。

The image is presented in a div as a background image that was set using the style attribute. 该图像以div形式显示为使用style属性设置的背景图像。

style="background-image: url("//img1.wsimg.com/isteam/ip/c77f0810-0ea1-4425-b963-0e5820c70a3f/5a18a70f-82dc-45b6-bfb8-8f8e7ff1c875.jpg/:/cr=t:0%25,l:0%25,w:100%25,h:100%25/rs=w:1400,h:400,cg:true,m/cr=w:1400,h:400,ax:c,ay:c");"

The id of the div container is generated automatically and changes between runs. div容器的ID是自动生成的,并且在运行之间会更改。

Is there anyway I can find the elements on the page that have a background image set in the HTML style attribute using plain javascript ? 无论如何,我可以使用普通的javascript在页面上找到在HTML样式属性中设置了背景图片的元素吗?

Try this: 尝试这个:

var elements = document.body.getElementsByTagName("*")

for (var i = 0 ; i < elements.length ; i++) {
 if (elements[i].style.backgroundImage) {
    // DO SOMETHING HERE
    alert(elements[i].id + " has background image")
 }
}

https://jsfiddle.net/ckrm2eLL https://jsfiddle.net/ckrm2eLL

暂无
暂无

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

相关问题 如何使用jquery查找包含与前缀匹配的data- *属性的元素 - how do I find elements that contain a data-* attribute matching a prefix using jquery 如何找到哪个 JavaScript 正在改变元素的样式? - How do I find which JavaScript is changing an element's style? 如何使用element.style.backgroundImage =“ url(filePath)”将背景图像添加到javascript? - How do I add a background image to javascript using element.style.backgroundImage = “url(filePath)”? 单击一个具有href属性的元素,然后在javascript中更改另一个元素样式,怎么做? - Clicked on an element which has href attribute, then change another elements style in javascript, how to do that? 如何通过javascript设置背景图片和渐变-在不使用dom style属性进行jquery的情况下 - how to set background image and gradient via javascript - without jquery using dom style attribute 如何找到所有具有样式背景图像的 div? - How to find all div which have style background-image? JavaScript中的背景图片样式属性 - Background-image style attribute in JavaScript 如何过滤包含AJAX请求的字符串的元素? - How do I filter elements which contain a string with an AJAX request? 如何使用javascript查找哪些html元素最近更改了背景颜色 - How to find which html elements have a recently changed background-color using javascript 如何使用JavaScript更改背景图片在网站上的位置? - How do I change the background image position on a website using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM