Javascript -将属性拉入数组/根据属性删除项
Javascript - Pull attribute into array/delete items based on attribute
我想将特定<div>
中所有<img>
的类拉入数组,然后使用这些类删除在不同<div>
中共享该类的第一个<img>
。
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();
}
}
});
我建议使用class以外的其他属性,例如:"数据类型"。
使用收集的属性值(例如。
var $grid = $('#grid');
// iterate over collected types
types.forEach(function(type)) {
// find within $grid the first <img> with data-type == type and remove it from DOM
$grid.find('img[data-type="' + type + '"]:eq(0)').remove();
}
你也可以一气呵成:
// iterate over source <img> set
$('.frame div img').each(function() {
// get current images type-attrib
var type = $(this).attr('data-type');
// find within $grid the first <img> with data-type == type and remove it from DOM
$grid.find('img[data-type="' + type + '"]:eq(0)').remove();
});
Try
$(function() {
var classes = $.map($(".frame div img"), function(v, k) {
return [$(v).attr("class")];
});
var d = [];
console.log($("#grid img").length);
$.each($("#grid img"), function(k, v) {
if ( classes.hasOwnProperty($(v).attr("class")) ) {
d.push(v); $("body").find($(d.slice(0, 1))).remove();
};
});
console.log($("#grid img").length);
});
jsfiddle http://jsfiddle.net/guest271314/yv95C/
相关文章:
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 未捕获的类型错误:无法读取属性'删除'的未定义
- 在不知道深度或父属性的情况下从对象中删除属性
- 一个正则表达式,用于从JS中的HTML标记中删除id、样式和类属性
- 如何从对象中删除属性
- 如何选择多个输入字段并删除所需的属性
- Es6:能够在设置/更新/删除对象属性时调用自定义方法
- 如果另一个属性相同,请删除该属性
- 为什么toStaticHTML删除data-*属性
- 使用jQuery添加和删除属性
- AngularJS-深层对象'属性删除
- 无法读取空放置区的属性“删除子项”
- 如何使用jquery按数据属性删除li标签
- JavaScript 属性删除如果为假,则不删除,如果为真循环
- 如何使用HTML数据集属性删除data-*属性
- Javascript -将属性拉入数组/根据属性删除项
- 如何使用select标签的title属性删除选中的选项
- jquery:通过指定数据属性删除元素
- 基于属性删除对象
- javascript属性删除