首页 > 技术文章 > JavaScript控制iframe中元素的样式

caixiaohua 2018-08-21 17:39 原文

//根据ID获取要操控元素
var deptObjs=document.getElementById("IFRAMEID").contentWindow.document.getElementById("TAGID");
//判断此元素是否存在
if(deptObjs!=null){
    //设置该元素的样式或其他属性
    deptObjs.setAttribute('style',' height: 20px !important;'); //!important用来提升指定样式条目的应用优先权
}

contentWindow

contentWindow属性是指指定的frame或者iframe所在的window对象。返回指定的iframe的窗口对象。

!important

!important用来提升指定样式条目的应用优先权,当html页面有样式冲突时,可以在需要的样式后面加上!important,提升优先级。避免样式冲突。

推荐阅读