JS子页面操作父页面
- Html/Css/Js
- 2020-07-23
- 16热度
- 0评论
parent.$("iframe").contents().find("#testbtn").click()
//在使用iframe的页面时,要操作这个iframe里面的DOM元素可以用:contentWindow、contentDocument
//jquery选择器获得iframe,先把jquery对象转换为DOM对象,或者使用get()方法转换,在得到iframe的window对象后接着使用jquery选择器进行页面操作。
parent.$("iframe")[1].contentDocument.getElementById("testbtn").click();
parent.$("iframe")[1].contentWindow.document.getElementById("testbtn").click();
parent.$("iframe")[1].contentWindow.$("#btable").bootstrapTable("refresh");
$('iframe', window.parent.document)[1].contentWindow.$("#btable").bootstrapTable("refresh");
//window.top是获取的顶层,即有多层嵌套iframe的时候使用。
top.$("iframe[name='iframeWindow']")[0].contentWindow.$("#inside_tableElement"); //这样才能获取到iframe里的元素
top.$("iframe[name='iframeWindow']").eq(0).$("#inside_tableElement"); //这是获取不到的
//可以通过这种方式向iframe页面传递参数,在iframe页面window.username就可以获取到值,username是自定义的全局变量。
$("#myiframe")[0].contentWindow.username="zhangsan";

鲁ICP备19063141号
鲁公网安备 37010302000824号