动态添加到javascript数组

时间:2014-07-16 06:07:11

标签: c# javascript asp.net arrays dynamic

我有一个javascript文件,用于从数组中获取图像的滑块。如何从代码后面添加图像URL。我想从数据库中获取图片网址,因此我想从后面的代码中将图像添加到数组中。我使用asp.net c#。 jquery文件如下所示:

$(function() {
    $('#ds_showcase').showcase({
        linksOn: 'images',
        css: {},
        animation: { autoCycle: true, type: 'horizontal-slider', interval: 4000, stopOnHover:true, speed: 1500, easefunction: 'easeOutBounce'},
        images: [
                  { url: 'Showcase_ds_images/lift-truck.jpg ', description: 'Title 2', link: '', target: '_self' },
                  { url: 'Showcase_ds_images/lifttruck2.jpg', description: 'Title 3', link: '', target: '_self' },
                  { url: 'Showcase_ds_images/lift-truck3.jpg', description: 'Title 4', link: '', target: '_self' },

        ],
        navigator: { position: 'bottom-right', orientation: 'horizontal', autoHide: false, showNumber: false,
            css: { padding: '10px'}, 
            item: { css: { height: '10px', width: '10px', backgroundColor: '#cccccc', borderColor: '#999999'},
                    cssHover: { backgroundColor: '#3399ff'},
                    cssSelected: { backgroundColor: '#3399ff', borderColor: '#3399ff'}
            }
        }

    });
});

1 个答案:

答案 0 :(得分:0)

您可以随时执行以下操作:

var jsArr = [];
<%foreach(string img in images){%>
   jsArr.push('<%=img%>');
<%}%>

此代码将从服务器生成带有硬编码字符串的js。 每个客户看到的js都是这样的:

 var jsArr = [];

    jsArr.push('http://server/img1.png');
    jsArr.push('http://server/img2.png');
    jsArr.push('http://server/img3.png');

在你的情况下 - 像这样:

$(function() {
    var jsArr = [];
    <%foreach(Image img in images){%>
       jsArr.push({url: '<%=img.url%>', description: '<%=img.desc%>', link: '', target:'_self'});
    <%}%>

    $('#ds_showcase').showcase({
        linksOn: 'images',
        css: {},
        animation: { autoCycle: true, type: 'horizontal-slider', interval: 4000, stopOnHover:true, speed: 1500, easefunction: 'easeOutBounce'},
        images: jsArr,
        navigator: { position: 'bottom-right', orientation: 'horizontal', autoHide: false, showNumber: false,
            css: { padding: '10px'}, 
            item: { css: { height: '10px', width: '10px', backgroundColor: '#cccccc', borderColor: '#999999'},
                    cssHover: { backgroundColor: '#3399ff'},
                    cssSelected: { backgroundColor: '#3399ff', borderColor: '#3399ff'}
            }
        }

    });
});