使用jQuery从字符串中删除最后一个逗号

时间:2022-08-27 17:16:39

I am using the Supersized jQuery plugin and need to remove the last comma from the list of images so it works in IE. The Supersized plugin does not work in IE if there is a comma after the last image, this is a known issue.

我正在使用Supersized jQuery插件,需要从图像列表中删除最后一个逗号,以便它在IE中工作。如果在最后一个图像后面有逗号,则Supersized插件在IE中不起作用,这是一个已知问题。

I am using Business Catalyst, so this is not PHP.

我正在使用Business Catalyst,所以这不是PHP。

This is how the list of images appear, with a trailing comma:

这是图像列表的显示方式,带有逗号:

{image : 'melbourne.jpg'},{image : 'tunnel.jpg'},{image : 'building.jpg'},

What would be the best way to do this?

最好的方法是什么?

    jQuery(function($){
            $.supersized({
                slide_interval          :   3000,       
                transition              :   1,          
                transition_speed        :   700,        

                slides                  :   [  // Slideshow Images  
                                               {module_webapps,9198,a template="/Layouts/WebApps/slide.tpl"}
                                            ]
            });
        });

And this is what /Layouts/WebApps/slide.tpl looks like. Basically just looping through the slider images...

这就是/Layouts/WebApps/slide.tpl的样子。基本上只是循环滑块图像......

{image : '{tag_bg image_value}'},

3 个解决方案

#1


5  

You can use regulare expression on your string like that :

您可以在字符串上使用regularre表达式:

var modifiedString = yourString.replace(/,\s*$/, '');

This will remove the last comma IF there is one and will also remove white space.

这将删除最后一个逗号,如果有一个逗号,也将删除空格。

#2


1  

Try substring to remove last comma

尝试使用substring删除最后一个逗号

var data = "{image : 'melbourne.jpg'},{image : 'tunnel.jpg'},{image : 'building.jpg'},";
    data = data.substr(0, data.length-1);

    console.log( data );

#3


0  

If Business Catalyst doesn't give you the flexibility to do the Django-style {if forloop.last},{endif} tags, consider changing your

如果Business Catalyst不能灵活地执行Django风格的{if forloop.last},{endif}标记,请考虑更改

]

into

{}]

or

undefined]

so there won't be a trailing comma. Note that your supersized plugin will need to know how to process these "incorrect" values.

所以不会有一个尾随的逗号。请注意,您的超大插件需要知道如何处理这些“不正确”的值。

#1


5  

You can use regulare expression on your string like that :

您可以在字符串上使用regularre表达式:

var modifiedString = yourString.replace(/,\s*$/, '');

This will remove the last comma IF there is one and will also remove white space.

这将删除最后一个逗号,如果有一个逗号,也将删除空格。

#2


1  

Try substring to remove last comma

尝试使用substring删除最后一个逗号

var data = "{image : 'melbourne.jpg'},{image : 'tunnel.jpg'},{image : 'building.jpg'},";
    data = data.substr(0, data.length-1);

    console.log( data );

#3


0  

If Business Catalyst doesn't give you the flexibility to do the Django-style {if forloop.last},{endif} tags, consider changing your

如果Business Catalyst不能灵活地执行Django风格的{if forloop.last},{endif}标记,请考虑更改

]

into

{}]

or

undefined]

so there won't be a trailing comma. Note that your supersized plugin will need to know how to process these "incorrect" values.

所以不会有一个尾随的逗号。请注意,您的超大插件需要知道如何处理这些“不正确”的值。