gpt4 book ai didi

javascript - 使用 attr() 获取背景图片 url

转载 作者:行者123 更新时间:2023-12-02 15:59:50 30 4
gpt4 key购买 nike

我使用 attr('style') 并能够获得以下值,

background-image: url(http://example.com/img/123456-bg.jpg); background-size: cover;

但是如何继续获取

123456-bg

最佳答案

尝试

var $div = $('#mydiv'),
bimg = $div.css('background-image');
var match = bimg.match(/\d+-bg/),
value = match ? match[0] : undefined;

snippet.log(value)
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<!-- To show result in the dom instead of console, only to be used in the snippet not in production -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="mydiv" style="background-image: url(http://example.com/img/123456-bg.jpg); background-size: cover;"></div>

关于javascript - 使用 attr() 获取背景图片 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31307711/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com