1.jQuery获取iframe中的元素
由于公司项目开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素、或者在iframe框架中使用父窗口的元素。
1.1 在iframe中获取父窗口的元素
// 格式:
$('#父窗口中的元素ID', parent.document).click();
// 实例:
$('#btnOk', parent.document).click();
1.2 在父窗口中获取iframe中的元素
// 格式:
$("#iframe的ID").contents().find("#iframe中的控件ID").click();
// 实例:
$("#ifm").contents().find("#btnOk").click();
1.3 iframe 父窗口和子窗口相互的调用方法集锦
HTML语法:<iframe name="myFrame" src="child.html"></iframe>
父窗口调用子窗口:myFrame.window.functionName();
子窗品调用父窗口:parent.functionName();
2.jQuery给iframe里的元素添加事件
$("#iframeId").on("load", function(event){ // 判断 iframe是否加载完成 这一步很重要
$("#divId",this.contentDocument).click(function(){ //添加点击事件
alert("就是这样");
});
});
传统的获取iframe内容的一些 参考代码:
// 已请求完成
let handleResult = function() {
let ret = '';
let err = '内部错误,无法显示此内容';
ajax_show_loading(waitId, 'none', null);
if (waitId === 'ajaxShowId') {
dialog_object().close(loadingIndex);
}
try {
ret = base_get_obj(ajaxFrameId).contentWindow.document.XMLDocument.text;
} catch (e) {
try {
ret = base_get_obj(ajaxFrameId).contentWindow.document.documentElement.firstChild.wholeText;
} catch (e) {
try {
ret = base_get_obj(ajaxFrameId).contentWindow.document.documentElement.firstChild.nodeValue;
} catch (e) {
ret = err;
}
}
}
console.log(ret);
if (typeof ret == 'undefined') {
ret = err;
}
}