2012-04-26 170 views
0

我有一个张贴到iFrame的表单。 iFrame内的网站是我无法控制的。不过,我想将iframe中的几张图片显示为“无”。iFrame中的访问元素返回null

<form id="testForm" method="post" target="testFrame"> 
    <input type="hidden" name="RequestXML" ID="RequestXML" value="<Request><RedirectURL>Account/TokenRequest</RedirectURL></Request>" 
</form> 

<iframe id="testFrame" name="testFrame" frameborder="0" style="width:1000px;height:500px"> </iframe> 

这是我在页面顶部脚本:

<script type="text/javascript"> 
    $(function() { 
    $("#testForm").attr("action", "http://externalwebsite.aspx"); 
    $('#testForm').submit(); 

    var iframe = document.getElementById('testFrame'); 
    var innerDoc = iframe.contentDocument || iframe.contentWindow.document; 
    //alert($("#testFrame").contents().find("body").html()); 
    //alert(innerDoc.innerHTML); 
    }); 
</script> 

我的两个警报在Chrome,IE和Firefox返回null。访问该iframe中的元素的最佳方式是什么? (在Chrome我不甚至可以看到该网站的HTML,当我尝试做“检查元素”,并查看HTML)

通过我做的这一切都在MVC4项目

+0

看看这个http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe。 – EnigmaMaster 2012-04-26 16:32:59

回答

0

假设的方式,这不是跨域问题,请尝试:

var body = $('#testFrame').contents().find('body'); 
0

由于此跨域有两个选项。

如果您有权访问iframe代码,那么您可以将XDM代码放在那里,也许easyXDM会有所帮助。

或者

你必须去服务器端,让你的php/asp.net等后端去获取页面,并获得它需要的数据。