javascript - Get CSS background-image url from ID - Stack Overflow

how to get the url from this ID<div id="image" style="background-image: url(testtes

how to get the url from this ID

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

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

how to get the url from this ID

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

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

Share Improve this question edited Oct 22, 2016 at 16:51 Yosvel Quintero 19.1k5 gold badges39 silver badges47 bronze badges asked Oct 22, 2016 at 16:32 MarksywMarksyw 691 gold badge2 silver badges6 bronze badges 2
  • 1 Possible duplicate of Can I get div's background-image url? – Mohammad Commented Oct 22, 2016 at 17:11
  • Does this answer your question? Get URL from background-image Property – Jason C Commented Sep 8, 2020 at 16:23
Add a ment  | 

5 Answers 5

Reset to default 1

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./test.jpg)">

Using jQuery:

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

You can do it like this

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

I sit this to get the value from element with inline style properties.

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./ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<div id="image" style="background-image: url(//test./test.jpg); height: 230px" class="testclass">Image</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745531328a4631708.html

相关推荐

  • javascript - Get CSS background-image url from ID - Stack Overflow

    how to get the url from this ID<div id="image" style="background-image: url(testtes

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信