HTML-获取iframe元素的正确方法

跨域相关文章
详解js跨域问题
JavaScript跨域总结与解决办法

解释最清楚的jsonp
What is JSONP all about?

首先测试需要服务器环境
http://www.dyn-web.com/tutorials/iframes/refs/iframe.php#f2

附带由iframe到父级的方法
http://stackoverflow.com/questions/1451208/access-iframe-elements-in-javascript

错误的方法:
http://java-my-life.iteye.com/blog/1275205
提示:Cannot read property 'getElementById' of undefined

    document.getElementById('test').onload = function(){
        var ifrm = document.getElementById('test');
        var doc = ifrm.contentDocument.getElementById('testa');
        console.log(doc)

        //console.log(window.frames['test'].document.getElementById('testa')) //此方法错误Uncaught TypeError: Cannot read property 'getElementById' of undefined
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容