跳过for循环中的第一个值

时间:2013-08-27 20:36:02

标签: javascript asp.net for-loop skip continue

我在javascript中有一个forloop,它有120个值,我想要的是,我总是想跳过循环所持有的第一个值,并且总是想要跳转到下一个循环将生成的值,因为我有几个如果条件,i的值不会总是0,1,2,3,4,5等..它可能是23,28,30,32等以下是我的源代码: 看看循环的最后一行。

for (i = 0; i < document.links.length; i++) {
    L = document.links[i];
    ext = (L.href.indexOf('?') == -1) ? L.href.slice(-4).toLowerCase() : L.href.substring(0, L.href.indexOf('?')).slice(-4).toLowerCase();
    if (ext != '.jpg' && ext != '.png' && ext != '.gif' && ext != 'jpeg') continue;
    if (a == 'sh' && L.className.toLowerCase().indexOf('shutter') == -1) continue;
    if (a == 'lb' && L.rel.toLowerCase().indexOf('lightbox') == -1) continue;
    if (L.className.toLowerCase().indexOf('shutterset') != -1)
        setid = L.className.replace(/\s/g, '_');
    else if (L.rel.toLowerCase().indexOf('lightbox[') != -1)
        setid = L.rel.replace(/\s/g, '_');
    else setid = 0, inset = -1;
    if (setid) {
        if (!shutterSets[setid]) shutterSets[setid] = [];
        inset = shutterSets[setid].push(i);
    }
    imgdetails = L.innerHTML;
    console.log(imgdetails);
    img1 = jQuery.trim(imgdetails);
    alttext = jQuery(img1).attr('alt');
    shfile = L.href.slice(L.href.lastIndexOf('/') + 1);
    T = (L.title && L.title != shfile) ? L.title : '';
    //alert(inset);
    shutterLinks[i] = {
        link: L.href,
        num: inset,
        set: setid,
        title: T,
        imagealt: alttext
    }
    //on below line, where i is being called i always want to skip the first value and always pass from second value.
    L.onclick = new Function('shutterReloaded.make("' + i + '");return false;');
}

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

从索引1开始而不是0(“for(i = 1 ; i&lt; document.links.length; i ++)”):

for (i = 1; i < document.links.length; i++) {
    L = document.links[i];
    ext = (L.href.indexOf('?') == -1) ? L.href.slice(-4).toLowerCase() : L.href.substring(0, L.href.indexOf('?')).slice(-4).toLowerCase();
    if (ext != '.jpg' && ext != '.png' && ext != '.gif' && ext != 'jpeg') continue;
    if (a == 'sh' && L.className.toLowerCase().indexOf('shutter') == -1) continue;
    if (a == 'lb' && L.rel.toLowerCase().indexOf('lightbox') == -1) continue;
    if (L.className.toLowerCase().indexOf('shutterset') != -1)
        setid = L.className.replace(/\s/g, '_');
    else if (L.rel.toLowerCase().indexOf('lightbox[') != -1)
        setid = L.rel.replace(/\s/g, '_');
    else setid = 0, inset = -1;
    if (setid) {
        if (!shutterSets[setid]) shutterSets[setid] = [];
        inset = shutterSets[setid].push(i);
    }
    imgdetails = L.innerHTML;
    console.log(imgdetails);
    img1 = jQuery.trim(imgdetails);
    alttext = jQuery(img1).attr('alt');
    shfile = L.href.slice(L.href.lastIndexOf('/') + 1);
    T = (L.title && L.title != shfile) ? L.title : '';
    //alert(inset);
    shutterLinks[i] = {
        link: L.href,
        num: inset,
        set: setid,
        title: T,
        imagealt: alttext
    }
    //on below line, where i is being called i always want to skip the first value and always pass from second value.
    L.onclick = new Function('shutterReloaded.make("' + i + '");return false;');
}

虽然,为什么你需要这样做仍然不清楚。