我正在寻找这个问题的更新答案 Parsing CSS background-image
那里的答案似乎是正确的,但不幸的是,它去除了渐变中停止点的百分比值(例如,请参阅我的评论)
所以我希望有人能帮助我的是一个更新的正则表达式,比如 joos 接受的答案,它不会从渐变中去除百分比停止点值......
或者也许还有其他方法?
最佳答案
好的,我已经完成了这似乎工作正常 - 可能不需要使用 jQuery,但我会发布更新...
splitBgImage : function(string) {
//split the background-image string into an array of characters
//iterate over the array of characters adding each to the bgimg variable
//if we encounter a ( then we increment brackets by one
//if we encounter a ) then we decrement brackets by one
//if we encounter a , then if brackets == 0 then we push the bgimg property to the bgimgs array and set the bgimg property back to ''
//if we are at the end of the array add whats left to bgimgs minus the ';'
var brackets = 0, bgimg = '', bgimgs = [], splitString = string.split(''), totalLength = jQuery(splitString).length;
jQuery.each(splitString, function(index, char){
if(char == '(')
{
brackets ++;
}
if(char == ')')
{
brackets -- ;
}
if(char == ',' && brackets == 0)
{
bgimgs.push(bgimg);
bgimg = '';
}
else
{
if(char != ';')
bgimg += char;
}
if(index == totalLength-1)
{
bgimgs.push(bgimg);
}
});
return bgimgs;
}
关于javascript - 解析 CSS 背景图像 2015,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28620520/