简体   繁体   English

遍历数组时,如何从符合特定条件的元素中添加/删除类?

[英]When looping through an array how do I add/remove classes from elements that fit a specific criteria?

Below I have code that loops through a list of items and I want to remove a class "hazardous" from div class item-left if element.hazardous == no. 下面,我有遍历项目列表的代码,并且如果element.hazardous == no,我想从div类item-left中删除一个“危险”类。 The way I have it set up right now is not working and I'm wondering if it might be because I need to find a way to target the specific non hazardous array element. 我现在设置的方式不起作用,我想知道是否可能是因为我需要找到一种针对特定的非危险阵列元素的方法。

What "hazardous" does in my CSS is it adds a solid red border if the hazardous class is active. 我的CSS中的“危险”功能是,如果危险类别处于活动状态,它将添加一个红色实线边框。 Otherwise hide the red border. 否则,隐藏红色边框。

function showProducts(items) {
    var itemsHTML = [];
    items.forEach(function(element) {
        var url = element.url ? element.url : 'http://placehold.it/100x100'
        itemsHTML.push('<li><div class="item-left hazardous" data-product-id="' + element.id + '"><p><span class = "prod-name">' +
            'Product: ' + element.product + '</p></span><span class="prod-loc">' +
            'Location: ' + element.location + '</span><span class="qty">' +
            'Quantity: ' + element.quantity + '</span></div>' +
            '<div class="item-right"><img src="' + url + '"></div></li>')
        if (element.hazardous == 'no') {
            $('.item-left').removeClass('hazardous')
        }
    });
    $('.results').html(itemsHTML) 
}

Instead of hardcoding the hazardous class in the first place, apply it conditionally before the markup is rendered to DOM: 不必首先对hazardous类进行硬编码,而是在将标记呈现给DOM之前有条件地应用它:

function showProducts(items) {
var itemsHTML = [];
items.forEach(function(element) {
    var classes = ['item-left', element.hazardous === 'no' ? '' : 'hazardous'].join(' ').trim() 
    var url = element.url ? element.url : 'http://placehold.it/100x100'
  itemsHTML.push('<li><div class="' + classes + '" data-product-id="' + element.id + '"><p><span class = "prod-name">' +
    'Product: ' + element.product + '</p></span><span class="prod-loc">' +
    'Location: ' + element.location + '</span><span class="qty">' +
    'Quantity: ' + element.quantity + '</span></div>' +
    '<div class="item-right"><img src="' + url +
    '"></div></li>')
});
$('.results').html(itemsHTML)

One of the problems you were facing was that you were trying to remove a class from a DOM element that didn't yet exist - at that point it was still a string. 您面临的问题之一是您试图从尚不存在的DOM元素中删除一个类-那时它仍然是一个字符串。

暂无
暂无

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

相关问题 如何从javascript中的数组中删除某些匹配条件的元素? - How do I remove certain elements matching criteria from an array in javascript? 遍历无序列表以添加和删除类 - Looping through unordered list to add and remove classes 当循环遍历JS数组的值,并且我删除了值时,我是否需要使用while而不是for? - When looping through values of a JS array, and I remove value, do I need to use while instead of for? 使用Splice()函数,如何删除特定的数组元素? - Using Splice() function, how do I remove specific array elements? 单击下一个和上一个时,如何从导航菜单中添加和删除类? - How do I add and remove classes from the navigation menu when clicking on next and previous? 如何在反应中迭代 DOM 元素并添加/删除类? - How to iterate through DOM elements in react and add/remove classes? 如何一次向一个数组添加(推送)项目(循环遍历数组) - How do I add (push) items to an array one at a time (looping through array) 如何正确地从另一个数组中删除一个元素数组? - How do I correctly remove an array of elements from another array? 如何在reactjs中单击时从数组中添加和删除元素? - How can i add and remove elements from array on click in reactjs? 如何使用 Javascript 通过单击向嵌套在 UL 中的多个子元素添加和删除类? - How do I add and remove classes to multiple child elements nested in an UL with a single click using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM