【发布时间】:2021-01-14 03:36:14
【问题描述】:
在 Wordpress 上试试这个:
在我的iframe 中,我有一个._2p3a 类,我想将其宽度更改为._2p3a {width: 100% !important;}。
使用 CSS 无法访问该类,所以我正在尝试使用 JavaScript:
我的 JS 代码:
function hello() {
let myiFrame = document.getElementById("iframe-css");
let doc = myiFrame.contentDocument;
doc.body.innerHTML = doc.body.innerHTML + '<style>._2p3a{width: 100% !important;}</style>';
}
//the iframe id > "iframe-css"
代码来源:https://redstapler.co/how-to-apply-css-to-iframe/
错误:
land_page.js?ver=1.0:4 Uncaught TypeError: Cannot read property 'body' of null
at hello (land_page.js?ver=1.0:4)
at HTMLIFrameElement.onload ((index):539)
下划线代码:
.body.innerHTML = doc.body.innerHTML + '<style>._2p3a{width: 100% !important;}</style>';
试过了:Using CSS to affect div style inside iframe (所有示例都出现错误“无效”)。
我正在 iframe 上使用 onload="hello(this)" 运行此功能。
任何其他建议我可以如何编辑该类以使其宽度为 100%??
【问题讨论】:
标签: javascript html jquery css iframe