经过完整的AJAX重装格 [英] AJAX reload div after complete

查看:124
本文介绍了经过完整的AJAX重装格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重装只是一个div一旦功能已被点击并执行。

  $('newThread')的addEvent('提交',addThread)。

功能addThread(五){
    e.stop();
    VAR threadRequest =新Request.JSON({
        网址:'?control.php行动=的CreateThread',
        的onSuccess:createThreadSuccess
    })后(本);
}

功能createThreadSuccess(){
    新元素('跨',{
        文:后的成功。
    })注入($(对答));
 

我一直在使用 location.reload(真); ,但我是正确的说这将刷新整个页面,而不仅仅是在div我在<? / P>

一旦 createThreadSuccess 已执行我希望的对答分区重装,我一直在使用的这个链接的意见,但它似乎并没有在一个的jsfiddle

请注意我使用MooTools的不是jQuery的

解决方案

您不能重新载入一个div。 A DIV 是对整个网页只是一个单一的元素,自己有没有它从加载的URL,所以它不能被重新加载。您可以设置/替换Ajax调用一个div的内容,但是这绝对不是'重装'​​ - 你需要明确定义的URL来加载其新的内容

如果您想从远程URL加载它明确替换 DIV 的内容,你应该使用的 Request.HTML 而不是 Request.JSON ,并提供元素(或它的ID)作为更新参数:

  
      
  • 更新 - (元素:默认为空)。要插入的元素请求的响应文本到请求完成后
  •   

如果你只是希望替换元素的内容,无需远程加载,你可以简单地做:

  $('ID--DIV的)设置('文字','文本放在元素中');
 

或者

  $('ID--DIV的)集(HTML,&LT; P&GT;的HTML结束了&LT; EM&GT;内&LT; / EM&gt;将DIV&LT; / P&GT;');
 

或者建立一个完整的子树像你一样的新元素通话了。

至于第二问题: location.reload()只是简写 window.location.reload()所以,是的,它会重新加载整个页面(或帧)。它可以因为一个窗口或框架实际上这是从装载。位置

I am trying to reload just a div once a function has been clicked and executed.

$('newThread').addEvent('submit', addThread);

function addThread(e){
    e.stop();
    var threadRequest = new Request.JSON({
        url: 'control.php?action=createThread',
        onSuccess: createThreadSuccess
    }).post(this);
}

function createThreadSuccess() {
    new Element('span',{
        'text':'Post successful.'
    }).inject($(threadList)); 

I have been using location.reload(true); but am I correct in saying this will reload the whole page and not just the the div I am after?

Once the createThreadSuccess has been executed I wish for the threadList div to reload, I have been using this link for advice but it doesn't seem to work in a jsFiddle

Please note I am using MooTools NOT jQuery

解决方案

You cannot 'reload a div'. A div is just a single element on an entire webpage, and on its own it has no URL it was loaded from, so it cannot be reloaded. You can set/replace the contents of a div with an Ajax call, but that's definitely not 'reloading' - you'd need to explicitly define the URL to load its new content from.

If you want to explicitly replace a div's contents by loading it from a remote URL, you should use Request.HTML instead of Request.JSON, and supply the element (or its ID) as the update parameter:

  • update - (element: defaults to null) The Element to insert the response text of the Request into upon completion of the request.

If you are just looking to replace the contents of an element without remote loading, you can simply do:

$('id-of-div').set('text', 'Text to be placed inside the element');

Or:

$('id-of-div').set('html', '<p>HTML ending up <em>inside</em> the div</p>');

Or build an entire subtree as you did with the new Element call already.

As for the secondary question: location.reload() is just shorthand for window.location.reload() so yes, it will reload the entire page (or frame). It can because a window or frame actually has a location it was loaded from.

这篇关于经过完整的AJAX重装格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆