Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript通过它获取div';背景图像是什么?_Javascript_Html_Css - Fatal编程技术网

Javascript通过它获取div';背景图像是什么?

Javascript通过它获取div';背景图像是什么?,javascript,html,css,Javascript,Html,Css,我有一个有这种风格的div <div style="position:absolute;top:0px;left:0px;width:2004px;height:700px;background-color:undefined;background-image:url(https://taxirondo.net-informatika.com/wp-content/uploads/2018/04/slajder1.jpg);background-repeat:no-repeat;backg

我有一个有这种风格的div

<div style="position:absolute;top:0px;left:0px;width:2004px;height:700px;background-color:undefined;background-image:url(https://taxirondo.net-informatika.com/wp-content/uploads/2018/04/slajder1.jpg);background-repeat:no-repeat;background-size:cover;background-position:center center;"></div>


如何获取此div并使用另一个div(通过其背景图像)对其进行appand?这是自动生成的div,我无法添加类或id。

一种方法是获取所有已分配样式的
div
,然后找到指定为其
样式的元素。backgroundImage

constdivs=document.queryselectoral('div[style]”);
常量url=https://taxirondo.net-informatika.com/wp-content/uploads/2018/04/slajder1.jpg';
常量元素=数组。从(divs)
.find(item=>item.style.backgroundImage.includes(url));
控制台日志(elem)

一种方法是获取所有已分配样式的
div
,然后找到已指定为其
样式的元素。backgroundImage

constdivs=document.queryselectoral('div[style]”);
常量url=https://taxirondo.net-informatika.com/wp-content/uploads/2018/04/slajder1.jpg';
常量元素=数组。从(divs)
.find(item=>item.style.backgroundImage.includes(url));
控制台日志(elem)

您可以使用css属性选择器:

document.querySelector('[style*="background-image:url(https://taxirondo.net-informatika.com/wp-content/uploads/2018/04/slajder1.jpg)"]');
通过这种方式,您将使用提供的背景图像选择元素的第一次出现。不需要遍历元素集合

console.log(document.querySelector('[style=“background image:url(https://taxirondo.net-informatika.com/wp-content/uploads/2018/04/slajder1.jpg)"]'));

所选DIV
您可以使用css属性选择器:

document.querySelector('[style*="background-image:url(https://taxirondo.net-informatika.com/wp-content/uploads/2018/04/slajder1.jpg)"]');
通过这种方式,您将使用提供的背景图像选择元素的第一次出现。不需要遍历元素集合

console.log(document.querySelector('[style=“background image:url(https://taxirondo.net-informatika.com/wp-content/uploads/2018/04/slajder1.jpg)"]'));

Selected DIV
是否必须由背景图像选择?你手头没有其他东西吗,比如在一个特定的祖先元素中的一个特定位置,或者类似的东西?它绝对必须由背景图像选择吗?你手头没有其他东西,比如特定祖先元素中的特定位置,或者类似的东西吗?它可以通过以下方式进行优化:
querySelectorAll('div[style*=“background image”][style*='https://taxirondo.net-informatika.com/wp-content/uploads/2018/04/slajder1.jpg“]”)
可以对其进行优化(?)这样:
querySelectorAll('div[style*=“background image”][style*=”https://taxirondo.net-informatika.com/wp-content/uploads/2018/04/slajder1.jpg“]”)