在网页中,一个页面可能由于业务需要嵌套几个iframe,由于功能的需要,需要在对应的iframe页面中操作父页面的标签/元素属性,那么就要使用到以下在iframe中查找父页面元素的方法。注意:jQuery方法需要提前引入封装库才能使用。
原生js方法定位父页面id选择器:parent.document.getElementById("id");
原生js方法定位父页面class选择器:parent.document.getElementsByClassName("class")[0];
jQuery方法定位父页面id选择器:$("#id",window.parent.document);
jQuery方法定位父页面class选择器:$(".class",window.parent.document);
//有时候iframe会嵌套好几层,那么嵌套两层时就是:
window.parent.parent.document.getElementById("id"); //以此类推
//嵌套好几层,直接找最顶层可以用这个方法
window.top.document.getElementById("id");
上一篇:最新网站接入支付宝支付的详细步骤
扫一扫 加微信咨询