我想将所有 <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/