AJAX显示/隐藏div

时间:2014-04-09 04:15:05

标签: javascript jquery html ajax dom

我想每隔3秒对页面执行一次ajax调用。 它会返回0(如果为false)或html代码段<div>Content</div>

我应该如何根据ajax返回的内容在页面上放置或删除该div?

2 个答案:

答案 0 :(得分:3)

使用 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>

答案 1 :(得分:2)

一种可能的方式:

HTML

<div id="one" style="display:none"></div>
<div id="two" style="display:block"></div>

现在在您的成功功能中设置适当的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';
    }
})