javascript - 在 Array JS 上完成
问题描述
我有一个数组var $test = $(".main-div").find(".some-class img");
如何if
使用 JS complete正确创建语句?
if ($test.complete) {}
只有当数组中的所有元素(img)时,我才需要执行一个函数complete == true
。
// 上面的例子不起作用,因为它是 jQuery 对象,并且它是一个数组
解决方案
true
如果every
与您的选择器匹配的图像已被浏览器完全加载,则将返回以下内容:
[...document.querySelectorAll(".main-div .some-class img")].every(i=>i.complete)
用作 - 语句中的条件if
:
if ([...document.querySelectorAll(".main-div .some-class img")].every(i=>i.complete)) {
// do stuff
}
[...]
由annodeList
返回,因此您可以在其上使用数组方法。它被称为阵列传播。document.querySelectorAll
array
every()
你也可以做
Array.from(document.querySelectorAll(".main-div .some-class img")).every(i=>i.complete)
或者,如果您需要支持支持every()
但不支持的浏览器[...]
:
Array.prototype.every.call(document.querySelectorAll(".main-div .some-class img"), function(i) { return i.complete })
推荐阅读
- arrays - 根据最长列vba的长度隐藏行
- php - 年龄计算 & 如果在里面,如果不工作 / php
- java - 如果同一个线程一次又一次地获取锁,那么 ReentrantLock 如何公平?
- python - 使用除法休息python索引超出界限的多个子图
- php - php jsonparsing错误浏览器上没有数据
- php - 当其他输入更改值时,Kartik Select2 加载 ajax
- python - strptime() 参数 1 必须是 str,而不是 Series 时间序列转换
- r - 如何将 lm() 应用于按因子拆分的数据集
- python - 带有多行标题 xls 文件的 Pandas read_csv
- asp.net-mvc - Ajax 表单发布后模型中的 ASP.NET MVC 刷新控件