检查每个元素内部是否包含内容,或者等到它不是JS时 [英] Check if every element has content inside or wait till it's not JS

查看:74
本文介绍了检查每个元素内部是否包含内容,或者等到它不是JS时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助以使我的示例正常工作,我是JS的新手,所以请不要为此烦恼:

I need some help to make my example work, I'm new to JS, so please don't trow a tomatoes at me for this:

function match_me(element) {
  return element.length > 0;
}

if (document.getElementsByClassName("test-class").every(match_me)) {
  func_1();
} else {
   //Set Interval and check every 100ms till every element
   //length is match, then do "func_1()"
   //or after 100x attempt clear set interval and do "func_2()"
}

基本上,我想检查页面上每个类名称为test-class长度> 0(它们存在并且内部包含内容)的元素,如果为true,则执行func_1(),如果不等到它们成立,如果出了问题(尝试100次后),请执行func_2().

Basically I want to check if every element on page with class name test-class length > 0 (they exist, and have content inside), if this is true, then do func_1(), if not wait till they are, if something went wrong (after 100 attempt) just do func_2().

当用户请求时,元素的内容会动态加载.

Content to the elements load dynamically when it's requested by user.

免责声明:我在示例中使用every只是为了说明我的想法.

Disclaimer: I use every in my example just to illustrate my idea.

有人可以帮助我使它正常工作吗?

Can somebody help me to make it work?

推荐答案

也许这就是您想要的

document.addEventListener('DOMContentLoaded', function(doc) {
  document.querySelectorAll('.test-class').forEach(function(test) {
     if (test && test.innerHTML && test.innerHTML.length) func_1()
     else func_2()`enter code here`
  });
 });

尚不清楚何时触发func_1的情况,因此请记住这一点

It wasn't very clear of the circumstances of when func_1 should be triggered, so keep this in mind

这篇关于检查每个元素内部是否包含内容,或者等到它不是JS时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆