使用jquery从内联css获取背景图像url
我需要与使用jquery从内联css获取背景图像url,jquery,html,css,Jquery,Html,Css,我需要与url(img/bg/06.jpg)相同的链接,但是jquery给了我一个与url相同的长链接(“file:///D:/WORKING%20FILE/One%20Landing%20Page/version/img/bg/06.jpg“” 这是我的密码: <div class="box_bg"> <div style="background-image: url(img/bg/06.jpg)" class="single_bg"></div>
url(img/bg/06.jpg)
相同的链接,但是jquery给了我一个与url相同的长链接(“file:///D:/WORKING%20FILE/One%20Landing%20Page/version/img/bg/06.jpg“”
这是我的密码:
<div class="box_bg">
<div style="background-image: url(img/bg/06.jpg)" class="single_bg"></div>
<div style="background-image: url(img/bg/07.jpg)" class="single_bg"></div
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
<script>
$('.box_bg .single_bg').each(function () {
var bg_img = $(this).css('backgroundImage');
$(this).click(function() {
window.alert(bg_img);
});
});
</script>
如果内联css中没有更多属性,您可以简单地使用:
$('.box_bg .single_bg').click(function () {
var bg_img = $(this).attr('style').replace('background-image: ','');
alert(bg_img);
});
演示:
如果您想在单击特定图像时获取图像url,则无需使用each()。是否仅使用内联样式,以及是否定义了除背景图像以外的其他属性?我仅使用内联样式。。没有