简体   繁体   English

这是在JavaScript中使用两个循环的正确方法吗

[英]Is this the correct way to use two loops in JavaScript

I am trying to check two things here, with two different for loops . 我试图在这里检查两件事,使用两个不同的for loops One if a element has a attribute , that works! 一个如果一个元素具有一个attribute ,那就行得通! And the second loop should check if the alt tag is actually empty. 第二个循环应检查alt标记是否为空。 I figured passing the first iterator value to the second would dispatch the second loop. 我认为将第一个iterator值传递给第二个iterator值将调度第二个循环。 But that doesn't work! 但这不起作用! Can someone help me out with this one? 有人可以帮我这个忙吗?

function altChecker() {
    var doc = document,

        getStartedBtn = doc.getElementById('getStartedBtn');

        EventUtility.addHandler(getStartedBtn, 'click', function() {

        var all = doc.getElementsByTagName("IMG");

        console.log('success!');

        for (var i = 0, max = all.length; i < max; i++) {
            if (all[i].hasAttribute('alt')) {
                console.log('Yes, this has a ' + all[i].nodeName + ' tag!');
            } else {
                console.log('Sorry ' + all[i].nodeName + ' tag, doesn\'t have an alt tag!');
            }

        }
        for (var j = i, max2 = all; j < max2; j++) {
            if(! $(all[j]).attr('alt')){
                console.log('This is empty');
            } else {
                console.log('This aint');
            }
        };

    });
}

You can do it in one loop. 您可以一站式完成。 Check for the value of alt after checking if the attribute exists. 检查属性是否存在后,检查alt的值。

Note that alt is a standard property of the img tag but not div . 请注意, altimg标签的标准属性,但不是div的标准属性。 In the example I used .getAttribute for the the non-standard property. 在示例中,我将.getAttribute用于非标准属性。 If I used img tags then .getAttribute could be replaced with .alt since that's a standard property of the tag. 如果我使用了img标签,则.getAttribute可以替换为.alt因为这是标签的标准属性。 see comments. 看评论。

 function altChecker() { var out = []; var doc = document; var all = doc.getElementsByTagName("div"); for (var i = 0, max = all.length; i < max; i++) { var id = all[i].id; if (all[i].hasAttribute('alt')) { out.push(id + ' has alt'); var value = all[i].getAttribute('alt'); if (value != "") { out.push(id + ' alt="' + value + '"'); } else { out.push(id + ' alt is empty'); } } else { out.push(id + ' does not have alt'); } } doc.getElementById('output').innerHTML = out.join("\\n"); } altChecker(); 
 <div id='a' alt='foo'>-</div> <div id='b' alt='bar'>-</div> <div id='c' alt=''>-</div> <div id='d'>-</div> <pre id='output'></pre> 

I don't think 2 loops are required 我认为不需要2个循环

function altChecker() {
    var doc = document,

    getStartedBtn = doc.getElementById('getStartedBtn');

    EventUtility.addHandler(getStartedBtn, 'click', function() {

        var all = doc.getElementsByTagName("IMG");

        console.log('success!');

        for (var i = 0; i < all.length; i++) {
            if (all[i].hasAttribute('alt')) {
              if (all[i].alt === '') {
                  console.log('this has a ' + all[i].nodeName + ' tag BUT it is empty!');
              } else {
                  console.log('Yes, this has a ' + all[i].nodeName + ' tag and it is NOT empty!');
              }
            } else {
                console.log('Sorry ' + all[i].nodeName + ' tag, doesn\'t have an alt tag!');
            }

        }
    });
}

You're missing your .length property in the 2nd if: 如果第二种情况,您将丢失第二个.length属性:

max2 = all

Should be max2 = all.length 应该是max2 = all.length

If you intended to loop though the all collection twice, you missed the length property on all in the second loop, and you need to start the j variable from 0. 如果您打算循环遍历all集合两次,则在第二个循环中错过了all的length属性,并且需要从0开始启动j变量。

for (var j = i, max2 = all; j < max2; j++) {

should be: 应该:

for (var j = 0, max2 = all.length; j < max2; j++) {

The two loops are completely independent and just iterate over the all collection; 这两个循环是完全独立的,并且仅遍历所有集合。 you write them in exactly the same way. 您以完全相同的方式编写它们。

Mistakes like that are why I usually use functional idioms for looping: 像这样的错误是为什么我通常使用函数式习惯用法进行循环:

$.each(all, function(idx, each){...});

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

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