HTML 代码
<ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-1714167228247329" data-ad-slot="8611448539" data-adsbygoogle-status="done" data-ad-status="unfilled">
我检查过 :https://www.w3schools.com/js/js_htmldom_elements.asp https://www.w3schools.com/js/js_htmldom_elements.asp并尝试过,但没有帮助。
我想要这个元素data-ad-status="unfilled"
使用 JavaScript 从 HTML 中获取。所以,我可以在 if else 语句中使用它。
Like
if (data-ad-status="unfilled") {
some fuctions;
}
- 正如你告诉我的,数据来自
async
我添加的方式setTimeOut()
函数在这里等待大约2秒。
- 我无法了解数据的来源,所以这次
2s
是硬编码的。更改它的值并尝试运行它。我相信它会解决您的问题。
- But use
asyn-await
功能和制作selectAd()
等待数据加载。
const selecteAd = () => {
const myDiv = document.querySelector("[data-ad-status='unfilled']");
const selectAllWithAttributeAdStatus = document.querySelectorAll("[data-ad-status]");
/*This(selectAllWithAttributeAdStatus) one is just for showing how to select elements with data-ad-status attribute */
//let adStatus = myDiv.getAttribute("data-ad-status");
selectAllWithAttributeAdStatus.forEach(ad => {
if ((ad.getAttribute("data-ad-status")) === "unfilled") {
ad.style.background = "red";
ad.style.height = "100px";
ad.style.width = "100px";
ad.style.display = "inline-block";
} else {
ad.style.background = "green";
ad.style.height = "100px";
ad.style.width = "100px";
ad.style.display = "inline-block";
};
})
}
const myTimeout = setTimeout(selecteAd, 2000);
<div class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-1714167228247329" data-ad-slot="8611448539" data-adsbygoogle-status="done" data-ad-status="unfilled"></div>
<div class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-1714167228247329" data-ad-slot="8611448539" data-adsbygoogle-status="done" data-ad-status="somethingelse"></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)