近期做一些非單頁應用的管理後臺頁面中,iframe父子頁面調用難以避免。
jquery使用,現在總結如下:
1.父調用iframe中的元素(一定要iframe是load狀態):
$('#iframe').load(function() {
$("#iframe").contents().find('.footer').html('88888')
//或者如下
$("#iframe")[0].contentWindow.$('.footer').html('000000');
// 或者如下
$($('#iframe').prop('contentWindow').document).find(".footer").html('111111');
//調用子頁面方法
$("#iframe")[0].contentWindow.childFn();
});
2.子頁面調用父頁面的方法
//調用父方法
window.parent.parentFn({name:'abc', age:555, list:[1,3,4]});
//查找父元素
$(window.parent.document).find('#ele')