admin管理员组文章数量:1431143
I have div that has this style
<div style="position:absolute;top:0px;left:0px;width:2004px;height:700px;background-color:undefined;background-image:url(.jpg);background-repeat:no-repeat;background-size:cover;background-position:center center;"></div>
How can I get this div and appand it with another div(by its background image)? This is automatic generated div and I can't add class or id.
I have div that has this style
<div style="position:absolute;top:0px;left:0px;width:2004px;height:700px;background-color:undefined;background-image:url(https://taxirondo-informatika./wp-content/uploads/2018/04/slajder1.jpg);background-repeat:no-repeat;background-size:cover;background-position:center center;"></div>
How can I get this div and appand it with another div(by its background image)? This is automatic generated div and I can't add class or id.
Share Improve this question asked May 15, 2018 at 11:55 Biljana GlumacBiljana Glumac 511 silver badge8 bronze badges 1- Does it absolutely have to be selected by the background image? Do you have nothing else at hand, like a specific position inside a specific ancestor element, or something like that? – C3roe Commented May 15, 2018 at 12:00
2 Answers
Reset to default 5You could use css attribute selector:
document.querySelector('[style*="background-image:url(https://taxirondo-informatika./wp-content/uploads/2018/04/slajder1.jpg)"]');
This way you will select the first ocurrence of an element with the supplied background image. No need to iterate thru a collection of elements.
console.log(document.querySelector('[style="background-image:url(https://taxirondo-informatika./wp-content/uploads/2018/04/slajder1.jpg)"]'));
<div style="background-image:url(https://taxirondo-informatika./wp-content/uploads/2018/04/slajder1.jpg)">Selected DIV</div>
More about attribute selectors
One way is to get all the div
s which have style assigned and then find the element which has the specified as its style.backgroundImage
:
const divs = document.querySelectorAll('div[style]');
const url = 'https://taxirondo-informatika./wp-content/uploads/2018/04/slajder1.jpg';
const elem = Array.from(divs)
.find(item => item.style.backgroundImage.includes(url));
console.log(elem);
<div style="position:absolute;top:0px;left:0px;width:2004px;height:700px;background-color:undefined;background-image:url(https://taxirondo-informatika./wp-content/uploads/2018/04/slajder1.jpg);background-repeat:no-repeat;background-size:cover;background-position:center center;"></div>
Please understand the implications of this code before proceeding:
- It does a check if the element's background contains the URL. Doesn't check the exact value.
- It has to iterate through the available elements to find the desired one, which may be a costly operation when there are many such elements.
本文标签: htmlJavascript get div by it39s background imageStack Overflow
版权声明:本文标题:html - Javascript get div by it's background image? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745506051a2661250.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论