admin管理员组文章数量:1430033
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
5 Answers
Reset to default 1You 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>
本文标签: javascriptGet CSS backgroundimage url from IDStack Overflow
版权声明:本文标题:javascript - Get CSS background-image url from ID - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745531328a2662074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论