1

ページ内の 1 つの div のみを 5 秒ごとに動的にリロードしたいと考えています。しかし、私の応答では、ページ全体のコンテンツを取得します...指定された div のコンテンツのみを取得するにはどうすればよいですか?

<script>
function ajaxrefresh()
{
    var xmlhttp;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            //Here i want only the content for the div from the response       
            document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
        else
        {
            //alert("state: "+xmlhttp.readyState)
            //alert("status: "+xmlhttp.status)
        }
    }

    xmlhttp.open("GET","http://${localHostAddress}:8080/adress",true);
    xmlhttp.send();

    var t=setTimeout(ajaxrefresh,5000);
}

window.load = ajaxrefresh();
</script>
4

4 に答える 4

1

Jqueryを使用できないため、サーバー側からのdivコンテンツのみを返すajax呼び出しに応答するのが最善の策だと思います。このようにして、応答サイズが小さくなり、高速になります...

リクエストのレスポンスを変更できない場合は、以下のコードを使用してください

if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
  //Here i want only the content for the div from the response  
  var mydiv= document.getElementById("myDiv"); 
  var doc=document.createElement("div");
  doc.innerHTML=xmlhttp.responseText;
  document.body.appendChild(doc); // Note append temp to document
 mydiv.innerHTML=doc.getElementById("theDivYouWant")
    .innerHTML;
  document.body.removeChild(doc);// Note remove temp from document
于 2013-05-16T07:27:45.543 に答える
0

以下のスクリプトからいくつかのアイデアが得られるかもしれません。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>

<script type="text/javascript">
       var auto_div_refresh = setInterval(function(){
   $('#load_data').load('fb_count.php').fadeIn("slow");
}, 10000); // refresh every 10000 milliseconds
</script>
于 2013-05-16T07:16:43.263 に答える