最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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>

发布评论

评论列表(0)

  1. 暂无评论