简体   繁体   English

将Javascript数组转换为函数的字符串

[英]Converting Javascript Array into String for Function

I would like to use an array i made to run .superslides function on 我想使用我制作的数组来在.superslides函数上运行

$(function() {
  $('#slides-1, #slides-2').superslides({
    hashchange: false
  });
});

here it is with the array i want to use 这是我要使用的数组

$(function() {
    $(slideShowArr).superslides({
        hashchange: false
    });
});

and, just in case, here is how the array was made 以防万一,这是数组的制作方法

$(".slideshow").each(function(i, el){
    el.id = 'slides-' + (i + 1);
    slideShowArr.push('#' + $(this).attr('id'));
});

so i need to pass slide show id's to .superslides function 所以我需要将幻灯片放映ID传递给.superslides函数

Use the join method of Array class to achieve this. 使用Array类的join方法可以实现这一点。

$(function() {
    $(slideShowArr.join(",")).superslides({
        hashchange: false
    });
});

Not sure whether you really want this: 不知道您是否真的想要这样:

$(function() {
    var slideShowArr = [];
    $(".slideshow").each(function(i, el){
        el.id = 'slides-' + (i + 1);
        slideShowArr.push('#' + $(this).attr('id'));
    });
    $(slideShowArr.join(", ")).superslides({
        hashchange: false
    });
});

because it makes absolutely no sense to iterate over a collection giving each item an id and then making a new selection from all the generated ids - just use the collection you already have! 因为遍历一个集合,为每个项目指定一个ID,然后从所有生成的ID中进行新选择,绝对没有意义-只需使用您已经拥有的集合即可! Better: 更好:

$(function() {
    $(".slideshow").prop("id", function(i) {
        return 'slides-' + (i + 1);
    }).superslides({
        hashchange: false
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM