javascript - 更改 iframe 中的 css 变量
问题描述
我遇到了以下问题。我正在使用 Angular 加载 Iframe。将 Angular 应用程序视为 A,将 Iframe 视为 B。我在 A 中加载 B。我使用 css 变量在 B 上设置颜色。在 A 上,我添加了一个颜色选择器。我想在 B 的正文中设置 A 的颜色选择器上选择的颜色。如何修改 Iframe 中 B 的主体颜色?作为参考,请考虑 WordPress 的自定义页面。我想做这样的事情
解决方案
你没有提供任何代码,所以我用纯 html 和 javascript 回答。
框架 B:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
:root{
--the-color:red;
}
</style>
</head>
<body>
<div style="color:var(--the-color)">
BBBB
</div>
</body>
</html>
您在框架 A(父)中的 javascript:
document.getElementById('fr1').contentWindow
.document.documentElement.style.setProperty('--the-color','blue')
这会将颜色从红色变为蓝色。
推荐阅读
- node.js - 通过 Express Validator 使用链验证来检查可选字段的存在
- python - 如何让 Conda 和 Virtualenv 在 mac OS Catalina 上工作?
- javascript - For循环无限运行
- c - 数据比 malloc 声明的大怎么办?
- spring - 无法将shell指向本地spring云数据流服务器?
- hadoop - 是否可以仅针对 reducer 任务增加容器大小?
- python - 根据包含集合的列合并两个数据框
- c# - 将新记录插入数据库时,WPF MVVM ListView 未更新
- python - 使用 Pandas 将 groupby.sum() 的结果映射到另一个数据帧?
- android - 如何在 Android Studio 预览窗口中显示使用合并标签创建的自定义视图