【问题标题】:Skipping first value in the forloop跳过 for 循环中的第一个值
【发布时间】:2013-08-27 20:36:01
【问题描述】:

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

谁能帮帮我。

【问题讨论】:

  • 你想要做的事情有点模棱两可完全
  • continue 关键字可用于跳过循环的一次迭代。如果您有可以检测是否跳过的逻辑,则可以像使用break;return; 一样使用continue;
  • for (var i = 1; etc?
  • @bfavaretto,我也是这么想的。不太清楚 “跳过第一个值” 的 OP 是什么意思
  • @NicholasV。我也不确定,只是删除了该评论以防 OP 正在寻找...

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多