// child.js
if (window.parent !== window) {
const sendHeight = () => {
window.parent.postMessage({
type: 'resize',
height: document.documentElement.scrollHeight
}, '*');
};
new ResizeObserver(sendHeight).observe(document.body);
sendHeight(); // 首帧立即发送
}// functions.php
add_action('wp_footer', function () {
if (!is_singular()) return;
?>
[/span>script
(function(){
const iframe = document.querySelector('iframe【data-resize】');
if (!iframe) return;
window.addEventListener('message', e => {
if (e.data?.type === 'resize') {
iframe.style.height = e.data.height + 'px';
}
});
})();
script
});[/span>iframe src="https://example.com/form" data-resizeiframeX-Frame-Options 会直接拦掉。scrollHeight,否则高度会跳变。height 设为 auto 作为兜底。wp_enqueue_script 加 defer,确保 DOM 已就绪。v2025.09.02-raft。愿这条龙舟,也能在你明天的河流里稳稳前行。child.js:(function () {
if (window.parent === window) return; // 防止独立打开时报错
const send = () => {
window.parent.postMessage({
type: 'resize',
height: Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight
)
}, '*');
};
const observer = new ResizeObserver(send);
observer.observe(document.body);
window.addEventListener('load', send); // 图片懒加载后重新计算
})(); 内引入:[/span>script src="/child.js" deferscriptfunctions.php,追加:add_action('wp_footer', function () {
?>
[/span>script
(function () {
const iframes = document.querySelectorAll('iframe【data-auto-resize】');
if (!iframes.length) return;
const resizeMap = new WeakMap(); // 防止重复绑定
const onMessage = (event) => {
if (event.data?.type !== 'resize') return;
for (const iframe of iframes) {
if (iframe.contentWindow === event.source) {
iframe.style.height = event.data.height + 'px';
break;
}
}
};
window.addEventListener('message', onMessage);
})();
script
});[/span>iframe src="https://example.com/form" data-auto-resizeiframe用“公路里程”插件让博客跑得更远:一位科技爱好者的趣事
WordPress iframe自适应教程:弹性视口策略
王总揭秘:云备份软件,数据安全新保障
备份电脑的快乐DIY:打造个人数据守护神
数据安全:dz备份数据库的重要性与实施指南
键盘上的叛逆:当搜索按钮罢工,我如何用幽默应对
备份快乐手册:软件备份应用的趣味指南
WordPress留言板设计:打造互动社区
WordPress区域流量控制策略详解
老炮解读:WordPress兼容模式那点事儿
给照片加个性签名:WordPress水印插件使用教程
WordPress悬浮效果轻松实现
WordPress地图插件安装全攻略:提升站点互动体验
轻松搞定内网穿透,让WordPress博客全球可见
WordPress模板二次开发实战:性能优化与搜索引擎友好
WordPress建站秘籍:打造流量王的小目标
本少教你轻松安装WordPress主机
WordPress文章密码保护策略详解
幽默解读wordpress博客地址