使用jQuery获取div的背景图像。是否有内置方法去除url()部分? [英] Getting a div's background image with jQuery. Is there an inbuilt method to strip out the url() portion?

查看:98
本文介绍了使用jQuery获取div的背景图像。是否有内置方法去除url()部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码获取 div 的背景图片。

I am using this code to get the background image of a div.

var bgImage = $('#content').css('backgroundImage');

这是返回 url%28http://example.com/images/ layout / content-trans.png%29

我知道你可以做 element.height()获取元素的高度,不带 px 追加( parseInt()也有效),所以我想知道如果有一个类似的方法让jQuery获得实际背景图像减去 url()元数据。

I know you can do element.height() to get an element's height without px appended (parseInt() also works), so I was wondering if there was a similiar method for jQuery to get the actual background image minus the url() meta data.

我想我可以使用正则表达式,比如 / url \((。*)\)/ ,但我宁愿先知道是否有内置的-in way。

I suppose I could use a regular expression, something like /url\((.*)\)/, but I'd rather know first if there is an built-in way.

推荐答案

url()是background-image值的一部分,所以我想你需要使用正则表达式替换。

url() is part of the value of background-image, so I guess you need to use regex replace.

var bgImage = $('#content').css('background-image').replace(/^url|[\(\)]/g, ''); 

参考: http://groups.google.com/group/jquery-en/browse_thread/thread/d866997cb206b35f

这篇关于使用jQuery获取div的背景图像。是否有内置方法去除url()部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆