简体   繁体   English

for循环返回数组中一个项的单个字母而不是数组中的每个项

[英]for loop returning individual letters of one item in array and not each item in the array

I have to arrays that I have made. 我必须使用我制作的数组。 One grabs some anchor tags and the other a set of ul's. 一个抓住一些锚标签,另一个抓住一组ul。 The anchor tags all have a name attribute and the ul's have id's that match the anchor tags name attribute. anchor标签都有一个name属性,ul的id与锚标签name属性相匹配。 In addition to this, all the ul's have the class ".students" and ".hidden". 除此之外,所有的ul都有“.students”和“.hidden”类。 (all the .hidden does is set the display:none) (所有.hidden都设置为display:none)

<div>
  <ul>
      <li><h4><a name="5th-grade">5th Grade</a></h4></li>
      <li><h4><a name="6th-grade">6th Grade</a></h4></li>
      <li><h4><a name="7th-grade">7th Grade</a></h4></li>
      <li><h4><a name="8th-grade">8th Grade</a></h4></li>
  </ul>
</div>
<div>
  <ul id="5th-grade" class="students hidden ">
      <li>Billy Bob</li>
  </ul>

  <ul id="6th-grade" class="students hidden">
      <li>Bob Sackamano</li>
  </ul>

  <ul id="7th-grade" class="students hidden">
      <li>Matt Blunt</li>
  </ul>
</div>

What I am trying to do is have it so when I click on one of the anchor tags, it will match it's corresponding name attribute to the ul with the same id, make it appear by removing the ".hidden" class, and then hide any other ul's that do not match by adding the ".hidden" class. 我要做的就是拥有它,所以当我点击其中一个锚标签时,它会将它的相应名称属性与具有相同id的ul匹配,通过删除“.hidden”类使其显示,然后隐藏通过添加“.hidden”类来匹配任何其他ul。

Here is what I have come up with so far using a little jquery and where I stopped: 以下是我到目前为止使用一个小jquery和我停止的地方:

    var aGradelist = $('#grade-list ul li a');
    var aStudents = $('.students');

    aGradelist.click(function(){
        var i = '#' + $(this).attr('name');

        $('.students'+i).removeClass('hidden');

        for(var j=0;j<aStudents.length;j++)
        {
           console.log(aStudents.attr('id')[j]);
        }

});

It's no problem getting the correct ul's to appear, but I couldn't add the ".hidden" class to the other ul's. 获得正确的ul是没有问题的,但是我无法将“.hidden”类添加到其他ul中。 I consoled it found that at least one of my problems is in the for loop, I am not going through each ul's in the aStudents array, but through the letters of the id of the first item in the aStudents array. 我安慰它发现我的问题中至少有一个是在for循环中,我不是通过aStudents数组中的每个ul,而是通过aStudents数组中第一个项的id的字母。

Am I even approaching this the right way? 我是否正确地接近这个? Have you got some ideas of how to do this? 你有一些关于如何做到这一点的想法吗?

Provided your html is valid (no duplicated IDs) this should do the trick. 如果您的html有效(没有重复的ID),这应该可以解决问题。

var aGradelist = $('#grade-list ul li a');
var aStudents = $('.students');
aGradelist.click(function() {
    aStudents.addClass('hidden');
    $('#' + $(this).attr('name')).removeClass('hidden');
});

jQuery iterates over the DOM elements out of the box when you call methods on it. 当您调用方法时,jQuery会对DOM元素进行迭代迭代。

Also, if you want to know what was wrong with your initial loop, you should use [j] before retrieving the id attribute which is a string. 另外,如果你想知道你的初始循环有什么问题,你应该在检索id属性(字符串[j]之前使用[j] So to retrieve the id property properly: 所以要正确检索id属性:

for(var j=0;j<aStudents.length;j++) {
   console.log(aStudents[j].id);
}

$()[j] is a shorthand for $().get(j) ( jQuery.fn.get docs ). $()[j]$().get(j)jQuery.fn.get docs )的简写。

Try this: 尝试这个:

var aGradelist = $('#grade-list ul li a');
var aStudents = $('.students');

aGradelist.click(function(){
    var i = '#' + $(this).attr('name');
    aStudents.addClass('hidden');
    $('ul' + i).removeClass('hidden');
}

What this does is add the hidden class to all the ul s and then remove it only from that ul which has the id of the anchor that was clicked 这样做是将hidden类添加到所有ul ,然后仅从具有被点击的anchor的id的ul删除它

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

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