javascript AJAX 显示/隐藏 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22952669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AJAX to show/hide div
提问by bockzior
I want to implement a ajax call to a page every 3 seconds.
It will either return 0
if false or a html snippet like <div>Content</div>
我想每 3 秒实现一次对页面的 ajax 调用。它要么返回0
false要么返回一个 html 片段,如<div>Content</div>
How should I proceed to place or remove that div on the page according to what ajax returns ?
我应该如何根据 ajax 返回的内容在页面上放置或删除该 div?
回答by Shijin TR
Use setInterval()
使用setInterval()
setInterval(ajaxCall, 3000);
function ajaxCall() {
$.ajax({url:url,
type:'html',
success:function(result){
if(result==0)
$('#content').hide();
else
$('#content').html(result).show();
}
});
}
<div id="content">Content</div>
回答by Brian
One possible way:
一种可能的方式:
html
html
<div id="one" style="display:none"></div>
<div id="two" style="display:block"></div>
Now in your success function set the appropriate div visible or hidden
现在在您的成功函数中设置适当的 div 可见或隐藏
ajax.request
({
// some code
success: function(response)
{
// here check the answer and show the div with id one
document.getElementById('one').style.display = 'block';
}
})