-3

我在javascript中有一个forloop,它有120个值,我想要的是,我总是想跳过循环保存的第一个值,并且总是想跳转到循环将生成的下一个值,因为我有几个if条件,值ofi不会总是 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;');
}

谁能帮我解决这个问题。

4

1 回答 1

0

从索引 1 而不是 0 开始(“for (i = 1 ; i < 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;');
}

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

于 2013-08-27T21:49:57.503 回答