分两种情况: 第一种: 获取iframe对象的JS函数在父页面上,如下
function getIframeByElement(element){ var iframe; $("iframe").each(function(){ if(element.ownerDocument === this.contentWindow.document) { iframe = this; } return !iframe; }); return iframe;}
使用的时候在iframe所在页面直接使用:
var iframe=window.parent.getIframeByElement(document.body);var iframeObj=$(iframe);
可以获取jquery对象。
第二种:
获取iframe对象的函数在iframe内部,如下:
function getIframeByElement(element){ var iframe; $("iframe", window.parent.document).each(function(){ if(element.ownerDocument === this.contentWindow.document) { iframe = this; } return !iframe; }); return iframe;}
使用的时候就很方便了,直接调用。
业务驱动技术,技术是手段,业务是目的