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

2016-05-25  本文已影响113人  前端历险记

跨域相关文章
详解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
    }
上一篇 下一篇

猜你喜欢

热点阅读