// 方案1:改用window.onload事件(等待所有资源加载) window.onload = function() { insertAd(); }; // 方案2:混合检测文档状态(推荐) (function() { if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', insertAd); } else { insertAd(); // 如果文档已经加载完成直接执行 } })(); function insertAd() { const container = document.getElementById('copyright'); if (!container) { console.error('找不到copyright元素'); return; } const adTemplate = ` `; container.insertAdjacentHTML('beforeend', adTemplate); // 确保广告脚本已加载 if (typeof adsbygoogle !== 'undefined') { (adsbygoogle = window.adsbygoogle || []).push({}); } else { console.error('Google广告脚本未加载'); } }