简体   繁体   中英

Get CSS background-image url from ID

how to get the url from this ID

<div id="image" style="background-image: url(/test.com/test.jpg)">

Has been searching google and stack overflow for this but only getting "get id from url" or current window location result only.

使用jQuery:

$('#image').css('background-image')

你可以这样

   $("#image").css("background-image");

You can use element style backgroundImage property:

 var img = document.getElementById('image'), url = img.style.backgroundImage; console.log(url); 
 <div id="image" style="background-image: url(/test.com/test.jpg)"> 

我坐在这里是为了从具有内联样式属性的元素获取值。

var imgUrl = $("#image").prop("style", "background-image");

 $(document).ready(function(){ var x=$("#image").css("background-image"); var imageurl = x.substring(5,x.length-2); alert(imageurl); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div id="image" style="background-image: url(//test.com/test.jpg); height: 230px" class="testclass">Image</div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM