Loading

jQuery操作iframe中js函数的方法小结

jQuery操作iframe中js函数的方法小结

1、jquery操作iframe中的元素(2种方式)

var tha = $(window.frames["core_content"].document).find("#trewuuu").html();
var thb = $("#core_content").contents().find("#trewuuu").html();

2、jquery调用iframe的js函数(带参数的会有返回值)

$("#core_content")[0].contentWindow.testIframe2("11");

3、jquery调用iframe的js函数(带参数的会有返回值)

$("#core_content")[0].contentWindow.testIframe2("11");

4、js调用iframe中的js函数(2种)

window.frames["core_content"].window.testIframe2("11");
document.getElementById("core_content").contentWindow.testIframe2("11");

注意

  1、因为JavaScript不支持跨域,所以iframe与父iframe或者和子iframe操作都需要在同一个域名下进行

  2、jquery调用iframe的js函数很多人会用 $("#core_content")[0].contents().testIframe2("11");这么写其实是错误的,必须要这么写$("#core_content")[0].contentWindow.testIframe2("11");contentWindow很关键

posted @ 2017-08-16 16:32  冯叶青  阅读(281)  评论(0编辑  收藏  举报