简体   繁体   English

JavaScript:如何在for循环中跳过数组中的当前项目? (继续?)

[英]JavaScript: How to skip over current item in array during a for loop? (continue?)

EDIT: I don't want to skip index 1. I want to skip the current (clicked on) element. 编辑:我不想跳过索引1。我想跳过当前(单击)元素。 Also, see below for more of the code as requested. 另外,请参阅下文以获取更多所要求的代码。 You'll see that I have a class CatListItem and five instances of that class in an array allCatListItems . 您会看到我有一个类CatListItem和在数组allCatListItems中该类的五个实例。

Here's some context for the question: I have a list of cats. 这个问题的背景如下:我有猫的清单。 When I click on a cat's name (list item), I want that cat's picture and other info to be appended to the page (got that down). 当我单击一只猫的名字(列表项)时,我希望将该猫的图片和其他信息附加到页面上(将其记录下来)。 When a cat is clicked, I also want any other cat that is being displayed to be hidden (that way there is only one cat on the screen at a time). 当单击一只猫时,我也希望隐藏其他正在显示的猫(这样,一次只在屏幕上显示一只猫)。

I'm trying to accomplish this with a for loop, but obviously if it iterates over every item in the array, then when I click an item, the cat being clicked will be hidden as well. 我正在尝试通过for循环来完成此操作,但是很显然,如果它遍历数组中的每个项目,那么当我单击一个项目时,被单击的猫也会被隐藏。

I want to skip the current item in the array and only run the code on the other items. 我想跳过数组中的当前项目,而只在其他项目上运行代码。 Using continue, I know I can skip a specific item (item 1 in the below example). 使用继续,我知道我可以跳过特定项目(以下示例中的项目1 )。 This will run my code on every item in the array except that at index one. 这将在数​​组的每个项目上运行我的代码,但索引1除外。 But how can I make that continue dynamic? 但是,如何使这种动态变化continue呢? Meaning... how can I hide all of the cats, except the one being currently clicked? 含义...除了当前被单击的那只猫以外,我该如何隐藏所有猫?

Here's the loop that skips index 1: 这是跳过索引1的循环:

CatListItem.prototype.hideCats = function() {
    allCatListItems.forEach(function(cat) {
        cat.a.addEventListener('click', function() {
            for (var i = 0; i < allCatListItems.length; i++) {
                if (i === 1) {
                    continue;
                }
                allCatListItems[i].img.className = 'hide';
            };
        });
    });
}

var allCatListItems = [
    catListItem1 = new CatListItem('El', 'images/el.jpg', 'el'),
    catListItem2 = new CatListItem('Widdle Baby', 'images/widdle-baby.jpg',     'widdle-baby'),
    catListItem3 = new CatListItem('Mama', 'images/mama.jpg', 'mama'),
    catListItem4 = new CatListItem('Legion', 'images/legion.jpg', 'legion'),
    catListItem5 = new CatListItem('Boy', 'images/boy.jpg', 'boy'),
];

EDIT: Here's a fiddle. 编辑:这是一个小提琴。 JSFIDDLE Click the names to see the functionality without the hideCats function. JSFIDDLE单击名称以查看没有hideCats函数的功能。 Then uncomment where it says to to see my issue. 然后取消注释该说的地方以查看我的问题。

I'm starting to think maybe a for loop isn't the best option? 我开始认为也许for循环不是最好的选择?

In that case compare the event.target(its the element clicked) 在这种情况下,比较event.target(单击其元素)

EDIT: allCatListItems[i] needs it's .a property attached to it in the if statement (this is what contains the anchor element). 编辑:allCatListItems [I]需要它.a连接到它的if语句属性(这是包含锚元素)。 This is because the event listener is grabbing an anchor tag, so e.target will be returning an anchor tag as well. 这是因为事件侦听器正在抓取锚标记,所以e.target也将返回锚标记。 The if statement will never return as true if you aren't comparing the same type of element. 如果您不比较相同类型的元素,则if语句永远不会返回true。

cat.a.addEventListener('click', function(e) {
    for (var i = 0; i < allCatListItems.length; i++) {
       if (allCatListItems[i].a === e.target) {
           continue;
       }
       allCatListItems[i].img.className += ' hide';
    }
});

Here is a jsfiddle, it doesn't use the same element names, but it should be doing what you want. 这是一个jsfiddle,它没有使用相同的元素名称,但是应该按照您想要的去做。 https://jsfiddle.net/5qb4rwzc/ https://jsfiddle.net/5qb4rwzc/

$('li').on('click', function() {
    var index = $(this).index();
  var items = document.getElementsByTagName('li');
  for(var i = 0; i < items.length; i++) {
    if(i === index) continue;
    items[i].style = "display:none;";
  }
});

Its really depend on how you call the function "hideCat". 它实际上取决于您如何调用函数“ hideCat”。 Realizing that each time that function is called, more eventListeners are add to every cat item. 意识到每次调用该函数时,都会向每个cat项目添加更多的eventListeners。 Each time a cat is clicked, more than one event fired. 每次单击猫,都会触发多个事件。 Perhaps you should re-structure how to attach eventListeners to each cat item. 也许您应该重新构造如何将eventListeners附加到每个cat项目。

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

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