【问题标题】:AJAX to show/hide divAJAX 显示/隐藏 div
【发布时间】:2014-05-22 01:43:40
【问题描述】:

我想每 3 秒执行一次对页面的 ajax 调用。 如果为 false,它将返回 0 或像 <div>Content</div> 这样的 html sn-p

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

【问题讨论】:

  • 你尝试了什么?

标签: javascript jquery html ajax dom


【解决方案1】:

一种可能的方式:

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';
    }
})

【讨论】:

  • 我正在考虑这样做,虽然 div 有很多图像并且 99% 的时间它都被隐藏,所以这不是最好的选择,性能方面。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多