如何使用 javascript 或使用 jQuery 刷新网页的一部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10086786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:38:31  来源:igfitidea点击:

How to refresh a part of a webpage using javascript or using jQuery?

javascriptjqueryjavascript-events

提问by Valli69

How can I refresh a particular part of a webpage with a time interval( not entire page)

如何以时间间隔(不是整个页面)刷新网页的特定部分

回答by M3HD1

Suppose you have a DIV in your your Web Page that you want to refresh :

假设您的网页中有一个要刷新的 DIV:

<div id="myDiv"> </div>

To refresh it using javascript you just have to select it and change the html code :

要使用 javascript 刷新它,您只需选择它并更改 html 代码:

document.getElementById("myDiv").innerHtml = "Your new html code to display"

If you want to deal with forms, database queries ... You have to use AJAX to call some php scripts for example without reloading the current page ...

如果你想处理表单、数据库查询……你必须使用 AJAX 来调用一些 php 脚本,例如无需重新加载当前页面……

回答by rohit

You can use Ajax for your purpose. suppose you want to check username availability before registering a user to your site.

您可以将 Ajax 用于您的目的。假设您想在将用户注册到您的站点之前检查用户名的可用性。

create a request object asynchronously

异步创建请求对象

function createRequest()

{

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;

}

函数创建请求()

{

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;

}

Next is the code to send a asynchronous request

接下来是发送异步请求的代码

function checkAvailability (username) {

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

}

功能检查可用性(用户名){

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

}

Track the response

跟踪响应

function showStatus () {

if(request.readyState == 4) {

if(request.status == 200) {

  var response = request.responseText;

          if(response == 1){

              //username available

          } else{

              //username not available

          } 

  }

}

}

函数显示状态(){

if(request.readyState == 4) {

if(request.status == 200) {

  var response = request.responseText;

          if(response == 1){

              //username available

          } else{

              //username not available

          } 

  }

}

}

回答by Shaun Hare

You are talking about AJAX

你说的是 AJAX

Look at http://api.jquery.com/jQuery.ajax/for jQuery

查看http://api.jquery.com/jQuery.ajax/的 jQuery

But please consider learning the underlying javascript language - you will be better for it in the long run here is a simple example http://www.degraeve.com/reference/simple-ajax-example.php

但请考虑学习底层的 javascript 语言 - 从长远来看,你会更好,这里是一个简单的例子 http://www.degraeve.com/reference/simple-ajax-example.php

The history behind ajax can be found here http://www.adaptivepath.com/ideas/ajax-new-approach-web-applications

ajax 背后的历史可以在这里找到http://www.adaptivepath.com/ideas/ajax-new-approach-web-applications