我真的很难理解这一点。我根据 img 标签的 src 中是否有单词 /resize/
选择所有图像标签(我认为它返回一个数组......?)。然后我想检查每个 src 以查看该 img 标记的 src 位置中是否指定了一个文件。如果图像确实存在,那么我想从数组中提取该图像,但它不会删除。它还说Uncaught TypeError: images.indexOf is not a function
。
这是我的代码 -
var images = [];
var images = $(' img[src*="/resize/"]');
console.log(images);
$.each(images, function(key, value){
var getFile = $(value).data('src');
$.ajax({
type: 'HEAD',
url: getFile,
success: function() {
var index = images.indexOf(key);
if (index > -1) {
images.splice(index, 1);
}
},
error: function() {
}
});
x++;
});
有人可以帮忙吗?这真是要了我的命了……
最佳答案
$
(或jQuery
)构造函数返回一个 jQuery 对象。 jQuery 对象的行为非常类似于具有长度、切片方法等的数组,以及索引为 0、1、2…等的元素。
但是 jQuery 对象不是真正的 javascript Array
并且没有许多 Array 方法,例如 join
、reverse
或者在你的情况下 indexOf
。
您可以使用 index
方法。 Read more about it on jQuery API Documentation
关于javascript - 在 jQuery 中从对象数组中取出一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42077612/