Javascript - 将属性拉入数组/根据属性删除项目

标签 javascript jquery arrays

我想将所有 <img> 的类拉入一个数组中特别是<div>然后使用这些类删除第一个 <img>在不同的 <div> 中共享该类。

到目前为止,我已经调用了原始数组:

var class = $('.frame div img').each(function() {
    return $(this).class;
}).get();

class.forEach(function(entry) {
    console.log(entry);
});

日志输出 <img></img> 的列表行。

在那之后,我陷入困境。

//Iterate through array and delete first <img> in #grid that has the same class, limit one per iteration.

// var img_class = $.each(class, function(key, value) {
//     console.log(value);
//     return $(this).attr('class');
// });

$('#grid img').each(function(){
    if($(this).attr('class') == img_class){
        $(this).remove();
    }
});

目标是:

  • 将类数组放入 img_class 变量
  • 仅删除第一个 <img>因为它遍历数组中的每个类

谢谢!

最佳答案

我不确定我是否理解正确,但是这样的东西有什么帮助吗?

var firstIDs = "";

$('.frame div img').each(function() {

    firstIDs += $(this).attr('id') + ",";

});

var SplitIDs = firstIDs.split(",");

$('#grid img').each(function(){

    for(var i = 0; i < SplitIDs.length; i++) {

        if($(this).attr('id') == SplitIDs[i]){

            $("#grid img #"+$(this).attr('id')+":first").remove();

        }

    }

});

关于Javascript - 将属性拉入数组/根据属性删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25142654/

相关文章:

javascript - 在 div 内随机移动图像

C++数组类问题

javascript - 如何在 div 中创建平滑的 img 鼠标悬停

javascript - 一次仅显示一个扩展 javascript 显示带切换的内容

jquery - 从 DIV 获取内部文本

javascript - 在 AngularJS 应用程序中使用 Lo-Dash 从 REST 返回总和

C++大字符串初始化及使用

javascript - Bootstrap 4 : Show active nav-item on Navbar collapse

javascript - 如何在 EmberJS 验收测试中调用输入和按钮?

javascript - 如何确保 2 个 <video> 完全同时播放(没有滞后)?