检查每个元素内部是否包含内容,或者等到它不是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