我想知道如何只刷新我网站中的特定元素,而不是整个网页?我正在谈论的元素是一个加载非常慢的Flash应用程序,可能会遇到连接超时.我想让用户只刷新该元素/ falsh应用程序.我怎么做?下面我有一个Ajax函数来更新
HTML元素,但不知道如何将它应用到我的情况.
<head> <script type="text/javascript"> function loadXMLDoc() { 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) { document.getElementById("myDiv").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); } </script> </head> <body> <div id="myDiv"> <h2>Let AJAX change this text</h2> </div> <button type="button" onclick="loadXMLDoc()">Change Content</button> </body>
解决方法
试试这个:
function reload(){ var container = document.getElementById("yourDiv"); var content = container.innerHTML; container.innerHTML= content; //this line is to watch the result in console,you can remove it later console.log("Refreshed"); }
<a href="javascript: reload()">Click to Reload</a> <div id="yourDiv">The content that you want to refresh/reload</div>
希望它有效.让我知道