修改iframe中的CSS样式

不跨域的情况下在父页面覆盖iframe中的css样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const style = document.createElement('style')
style.type = 'text/css'
style.innerText = `
body{
font-size: 14px;
}
img{
display: block;
}
a{
display: inline-block;
color: #303133;
border: 1px solid #ddd;
border-right: 0;
border-left: 0;
padding: 10px;
margin: 10px;
}
`

document.getElementById('ifrm').onload = () => {
document.getElementById('ifrm').contentWindow.document.getElementsByTagName('head')[0].appendChild(style)
}