[英]How to refresh a part of a web page using Javascript or using jQuery?
如何以時間間隔(不是整個頁面)刷新 web 頁面的特定部分?
假設您的 Web 頁面中有一個要刷新的 DIV:
<div id="myDiv"> </div>
要使用 javascript 刷新它,您只需 select 它並更改 html 代碼:
document.getElementById("myDiv").innerHtml = "Your new html code to display"
如果你想處理 forms,數據庫查詢......你必須使用 AJAX 來調用一些 php 腳本而不重新加載當前頁面......
您可以將 Ajax 用於您的目的。 假設您想在將用戶注冊到您的站點之前檢查用戶名可用性。
異步創建請求 object
function 創建請求()
{
try{
request=new XMLHttpRequest();
} catch(tryMS){
try{ request=new ActiveXObject("Msxml2.XMLHTTP"); } catch(otherMS){ try{ request=new ActiveXObject("Microsoft.XMLHTTP"); } catch(failed) { request=null; } } } return request;
}
接下來是發送異步請求的代碼
function checkAvailability(用戶名){
request=createRequest(); if(request==null){ alert("Ajax request not possible on your browser"); return; } var url="checkAvailability?username="+username; request.open("GET", url, true); request.onreadystatechange = showStatus; request.send(null);
}
跟蹤響應
function 顯示狀態 () {
if(request.readyState == 4) { if(request.status == 200) { var response = request.responseText; if(response == 1){ //username available } else{ //username not available } } }
}
你說的是 AJAX
看看http://api.jquery.com/jQuery.ajax/ jQuery
但請考慮學習底層 javascript 語言 - 從長遠來看你會更好這里是一個簡單的例子http://www.degraeve.com/reference/simple-ajax-example.php
ajax 背后的歷史可以在這里找到http://www.adaptivepath.com/ideas/ajax-new-approach-web-applications
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.