简体   繁体   English

使用Ajax重新加载/刷新div

[英]Reloading/refreshing div with Ajax

I'm trying to put a piece of jquery together to show a hidden div and at the same time, refresh the parent div so that the javascript can amend and display the new height. 我正在尝试将一段jQuery放在一起以显示隐藏的div,同时刷新父div,以便javascript可以修改并显示新的高度。

This is what i have so far but after some research have found than to refresh a div you have to use ajax and was wondering if anyone could lend a hand. 这是我到目前为止的内容,但是经过一些研究发现,要刷新div,您必须使用ajax,并且想知道是否有人可以伸出援手。

var $r = jQuery.noConflict();
  $r(document).ready(function() {
      $r('#open').click(function () {
        $r('#expandable-2').show('slow');
        $r(this).load(location.href + '.panel > *');
      });
       $r('#close').click(function () {
        $r('#expandable-2').hide('1000');
        $r(this).load(location.href + '.panel > *');
      });
  });

So far I have this, 到目前为止,我有这个

  • a link with the id's of open and close 具有打开和关闭ID的链接
  • once clicked they give the css property of display block and display none to a div expandable-2 单击后,它们赋予显示块的css属性,并且不显示给div expandable-2
  • the part I'm stuck on is the refreshing of the parent div .panel in order to show the displayed div correctly. 我卡住的部分是刷新父div .panel,以便正确显示显示的div。

Reason being is that I'm currently using the CodaSlider script in my page and the height is dynamically brought in depending on how much content is in the container. 原因是我当前在页面中使用CodaSlider脚本,高度根据容器中的内容量动态增加。 Now in order for the new content to be displayed when clicked open, the container needs to be refreshed thus showing the new content with a new height. 现在,为了在单击打开时显示新内容,需要刷新容器,从而以新高度显示新内容。

Hope that makes sense but any help would be awesome. 希望这是有道理的,但是任何帮助都会很棒。

to "refresh" a div you just need to do: 要“刷新” div,您只需要执行以下操作:

document.getElementById("myDiv").innerHTML = new_content;

new_content may be the html returned by your ajax call but you definitely DON'T need an ajax call to "refresh" a div. new_content可能是ajax调用返回的html,但您绝对不需要ajax调用来“刷新” div。

You can change html of a div with html method of jQuery. 您可以使用jQuery的html方法更改div的html

$r(".content-div").html("Hello world");

If you want to load an html from server with ajax, you should use load function as below: 如果要使用ajax从服务器加载html,则应使用如下的load函数:

$r(".content-div").load("ajax/users.html");

There is a clear mess with your code. 您的代码有明显的混乱。 Just a few tips: 几点提示:

  • If your parent div contains #expandable-2, then you won't be able to do the ajax call and show the animation at once, because the ajax call is gonna destroy the #expandable-2 element while it is appearing/disappearing. 如果您的父div包含#expandable-2,则您将无法进行ajax调用并立即显示动画,因为ajax调用会在#expandable-2元素出现/消失时破坏它。
  • The proper way of loading external code through ajax into a div is: 通过ajax将外部代码加载到div中的正确方法是:

     $r('.panel').load('/path/to/url'); 

    This will load the response of '/path/to/url' into the '.panel' div. 这会将“ / path / to / url”的响应加载到“ .panel” div中。 Make sure that '/path/to/url' only returns the new content of the div , otherwise you cannot use $r('.panel').load, but an indirect approach must be used instead (ie: $r.ajax) 确保“ / path / to / url”仅返回div的新内容 ,否则不能使用$ r('。panel')。load,但必须使用间接方法(即:$ r.ajax) )

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

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