用jquery获取iframe里面的内容,为什么会出现这个错误 Cannot read property 'contentWindow' of null"

用jquery获取iframe里面的内容,为什么会出现这个错误 Cannot read property 'contentWindow' of null",第1张

 // var i=$(documentgetElementById("#iframeId")contentWindowdocumentbody)find("bdbriwrapper a")length;

 

  var i=$("#iframeId")contents()find("bdbriwrapper a")length;

  // 这么改试一试

你写的语法完全错误

initPagerList 为 iframe 页面内的方法。

<html>

<head>

 <title></title>

 <script>

  $(function(){

    /

     说明: iframeID 为iframe的ID;

     initPagerList 为 iframe 页面内的方法。

    /

    //调用iframe页面iframeID中的initPagerList方法

    $(windowparentdocument)contents()find("#iframeID")[0]contentWindowinitPagerList();

    //获取iframe页面iframeID中的文本框txtControlID对象

    var txtControlObj=$(windowparentdocument)contents()find("#iframeID")[0]contentWindowfind("#txtControlID");

    //为iframe页面iframeID中的文本框txtControlID赋值

    txtControlObjval("我就是你需要设置的内容值!");

  });

 </script>

</head>

<body>

</body>

</html>

最近在开发时遇到使用了Iframe的情况,在一个父窗口里使用了Iframe,之后又想在父窗口中使用JQuery来获得Iframe里的元素,之前有使用过,久没使用就忘了,今日记录下来。

格式:

事例

格式:

事例:

格式:

事例:

格式:

事例:

1、你取得iframe的document的前提为这个iframe的地址是你域内的页面,

比如你设src为百度,取这个iframe的document时就会报拒绝访问的错误

2、我试过了 documentgetElementById('myf')contentWindowdocument 在ie与谷歌都行的

3、documentframes('iframename')document 这个方法只能在ie下面取得到

4、用火狐的firebug看看吧,有什么错都会显示出来,莫名其妙的事情什么时候都可能发生,

有时我直接取取window都报undefined

$(function(){//相当天body的 onload,要等页页面加载完毕后才能找到iframe

 $("#hong")contents()find("body")append("I'm in an iframe!");

});

  </script>

  <iframe width="1198" height="729" name="hong" id="hong" src="" scrolling="no" style="text-align:center"></iframe>

以上就是关于用jquery获取iframe里面的内容,为什么会出现这个错误 Cannot read property 'contentWindow' of null"全部的内容,包括:用jquery获取iframe里面的内容,为什么会出现这个错误 Cannot read property 'contentWindow' of null"、jquery怎么获取当前页面iframe的id、JQuery获取Iframe中的元素等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9350614.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存