简体   繁体   English

用jQuery刷新div

[英]refresh div with jquery

I'd like to refresh a div. 我想刷新一个div。 It should have new information from the server on it. 它应该具有来自服务器的新信息。 All the other answers assume that you've gotten the new data from your $.ajax request and tell you to load that data onto your div, hide it, and show it again, like so: 所有其他答案都假定您已经从$ .ajax请求中获取了新数据,并告诉您将该数据加载到div上,将其隐藏并再次显示,如下所示:

$("#panel").hide().html(data).fadeIn('fast');

I know, I know, I probably should just get data with Ajax. 我知道,我知道,我可能应该只使用Ajax获取数据。 But right now, I want to just refresh the div, without refreshing the page, and without putting new HTML into the div. 但是现在,我只想刷新div,而不刷新页面,也不要将新的HTML放入div。 Is this possible? 这可能吗?

I want to just refresh the div, without refreshing the page ... Is this possible? 我只想刷新div,而不刷新页面...这可能吗?

Yes, though it isn't going to be obvious that it does anything unless you change the contents of the div. 是的,尽管除非更改div的内容,否则它不会做任何事情是显而易见的。

If you just want the graphical fade-in effect, simply remove the .html(data) call: 如果只需要图形淡入效果,只需删除.html(data)调用即可:

$("#panel").hide().fadeIn('fast');

Here is a demo you can mess around with: http://jsfiddle.net/ZPYUS/ 这是一个您可以弄乱的演示: http : //jsfiddle.net/ZPYUS/

It changes the contents of the div without making an ajax call to the server, and without refreshing the page. 它可以更改div的内容,而无需对服务器进行ajax调用,也无需刷新页面。 The content is hard coded, though. 但是,内容是硬编码的。 You can't do anything about that fact without contacting the server somehow: ajax, some sort of sub-page request, or some sort of page refresh. 如果不以某种方式联系服务器,您将无法对该事实做任何事情:ajax,某种子页面请求或某种页面刷新。

html: 的HTML:

<div id="panel">test data</div>
<input id="changePanel" value="Change Panel" type="button">​

javascript: javascript:

$("#changePanel").click(function() {
    var data = "foobar";
    $("#panel").hide().html(data).fadeIn('fast');
});​

css: CSS:

div {
    padding: 1em;
    background-color: #00c000;
}

input {
    padding: .25em 1em;
}​

I tried the first solution and it works but the end user can easily identify that the div's are refreshing as it is fadeIn(), without fade in i tried .toggle().toggle() and it works perfect. 我尝试了第一个解决方案,它可以工作,但是最终用户可以轻松地识别div正在刷新,因为它是fadeIn(),而我尝试使用.toggle()。toggle()却没有褪色,并且效果很好。 you can try like this 你可以这样尝试

 $("#panel").toggle().toggle(); 

it works perfectly for me as i'm developing a messenger and need to minimize and maximize the chat box's and this does it best rather than the above code. 当我正在开发Messenger时,它对我来说非常理想,并且需要最小化和最大化聊天框,并且这样做比上面的代码更好。

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

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