简体   繁体   English

如何将Ajax响应加载到iframe中而不是div中?

[英]How to load ajax response in to iframe instead of div?

I am loading ajax response to div but now i want to load it to middle iframe i tried the following but it never work! 我正在将ajax响应加载到div,但现在我想将其加载到中间iframe,我尝试了以下操作,但是它永远无法正常工作! could you guys tell me what i am doing wrong ? 你们能告诉我我做错了吗? Thanks 谢谢

<script type="text/javascript">// <![CDATA[

    $(document).ready(function() {
    $.ajaxSetup({ cache: false }); 
    setInterval(function() {
    //$('#divToRefresh').load('./doit.php');

    $("#iframe").attr('blank.php','./doit.php'); //change url
    }, 20000); 
    });
    // ]]></script>


<iframe name="top" src="blank.php" id="top"  scrolling="no" noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="79"></iframe>
<iframe NAME="middle" src="blank.php" id="middle" noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="238" scrolling="auto"></iframe>
<iframe NAME="foot" src="blank.php" id="foot"  scrolling="no"  noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="200"></iframe>


<div id="divToRefresh">Loading ...</div>

The attr is used to replace an attribute, you are not stating which attribute to replace you are stating the value of that attribute attr用于替换属性,不是在说明要替换的属性,而是在说明该属性的值

it should be something like: 它应该是这样的:

$("#middle").attr('src','./doit.php'); //change url

However this is not ajax... it is plain simple iframe technique in other words the $.ajaxSetup({ cache: false }); 但这不是ajax ...这是简单的iframe简单技术,换句话说就是$ .ajaxSetup({cache:false}); is not used in this case. 在这种情况下不使用。

You ajax call is wrong or incomplete. 您的ajax调用错误或不完整。 If you want to fetch data using ajax; 如果要使用ajax获取数据,请执行以下操作: you should read the jQuery.ajax() documentation . 您应该阅读jQuery.ajax()文档 But first, this and this or this . 但是首先是thisthisthis

Also read the .attr() documentation . 另请阅读.attr()文档 The first parameter should be the attribute name and the second should be the value. 第一个参数应该是属性名称,第二个参数应该是值。

But; 但; it looks like you want to redirect the page in that iframe . 看来您想在该iframe重定向页面。
If that's the case, your selector should be #middle : 如果是这样,您的选择器应为#middle

$("#middle").attr("src", "./doit.php");

You can also do it via a link (without jQuery or javascript) like this: 您也可以通过这样的链接(无需jQuery或javascript)来做到这一点:

<a href="./doit.php" target="middle">Do It!</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM