How can I use load() to refresh a div without duplicating the element?

后端 未结 3 1992
长情又很酷
长情又很酷 2021-01-13 11:59

I use this code to refresh a div with the id=\"my\":



        
相关标签:
3条回答
  • 2021-01-13 12:18

    You could try:

    <script type="text/javascript">
    function recp() {
      setInterval(function() 
        {
            $("<div/>").load(location.href+ ' #my', function(response, status, xhr) {
                $("#my").replaceWith(response);
                $(this).remove();
            });
        });
    }
    </script>
    
    0 讨论(0)
  • 2021-01-13 12:27

    add another div as a container and load content to it, like that.

    <script type="text/javascript">
    function recp() {
      setInterval(function() 
      {
        $("#result").load(location.href+ ' #my');
      });
    }
    </script>
    
    <div id="result">
      <div id="my"><?php echo date('a:i:s'); ?></div>
    </div>
    
    0 讨论(0)
  • 2021-01-13 12:27

    Alter your code to use $.get() instead of .load()

    <script type="text/javascript">
    function recp() {
      setInterval(function() {
          $.get(location.href, function(data){ 
              $('#my').empty().append( $(data).find('#my').children() );
          });
      });
    }
    </script>
    
    0 讨论(0)
提交回复
热议问题