简体   繁体   English

检查元素是否包含另一个

[英]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 . 问题是我有时会得到错误的答案,因为如果pi元素尚未加载,那么我会得到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.

相关问题 如何通过标签检查元素是否包含另一个元素? - How to check whether element contains another element by tag? Javascript:检查数组元素是否包含来自另一个数组的元素 - Javascript : Check array element contains element from another array 检查数组是否包含另一个数组的每个元素 - check whether array contains every element of another array or not 检查嵌套数组是否包含JavaScript中另一个嵌套数组的任何元素 - Check if a nested array contains any element of another nested array in JavaScript 检查一个数组是否包含 JavaScript 中另一个数组的任何元素 - Check if an array contains any element of another array in JavaScript 检查 object 是否包含 JavaScript 中另一个 object 的任何元素 - Check if an object contains any element of another object in JavaScript 检查一个数组是否包含另一个数组的任何元素,并且在JavaScript中为空 - Check if an array contains any element of another array with some null in JavaScript 检查一个数组是否包含另一个数组的任何元素并突出显示另一个数组中的元素 - React native - Check if an array contains any element of another array and highlight the ones from another array - React native 如何检查文档是否包含元素 - How to Check if Document Contains an Element 如何检查元素是否包含图像? - How to check if an element contains an image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM