[英]Check if element contains another
I have this code: 我有以下代码:
$('#detail div p').each(function() {
if ($(this).children("i").length) {
var TheVar = "Yes";
MyData.push(TheVar);
} else {
var TheVar = "No";
MyData.push(TheVar);
};
});
The problem is that I sometimes get an incorrect answer, because if the i
element inside p
is not yet loaded, then I get No
instead of Yes
. 问题是我有时会得到错误的答案,因为如果
p
的i
元素尚未加载,那么我会得到No
而不是Yes
。 Is there any solution to wait until the dom is loaded? 有什么解决方案可以等到dom加载完毕吗?
Best Regards 最好的祝福
Since you are running this in cheerio and have no control over any asynchronous data loading, try setting a 5 second delay (or longer) 由于您是在cheerio中运行的,并且无法控制任何异步数据的加载,因此请尝试将延迟设置为5秒(或更长时间)
setTimeout(function(){
// try now
},5000);
You could also use an interval timer to check if certain elements exist and when they do cancel the interval timer 您还可以使用间隔计时器来检查某些元素是否存在以及何时取消间隔计时器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.