页面使用Ajax没有jQuery的更新部分 [英] Update portion of page using Ajax without jQuery

查看:139
本文介绍了页面使用Ajax没有jQuery的更新部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该网页主要是在PHP和只是轻轻用任何JavaScript,所以我不希望使用jQuery时,它必须是不可能完成的。我想更新页面的部分,但我不是很有经验的阿贾克斯。 jQuery的方法后,使用该页面的URL以#sectionid。

The page is mostly in PHP and only lightly uses any JavaScript, so I do not wish to use jQuery when it must be possible without it. I want to update a portion of a page, but I'm not very experienced with Ajax. jQuery's method was to use the page's URL with "#sectionid" after it.

推荐答案

所有你需要知道的信息,以手工卷自己是在这里:

All the information you'd need to know to hand-roll your own is here:

https://developer.mozilla.org/en/XMLHtt$p$pquest

您会感兴趣的发送打开响应的readyState 的onreadystatechange 文档的部分。

You'll be interested in the send, open, response, readyState and onreadystatechange sections of the documentation.

的onreadystatechange 将火每当你的要求改变readyState的。一旦你的的readyState 更改为完成您可以检查您收到的响应。

onreadystatechange will fire whenever the readyState of your request changes. Once your readyState has changed to done you can check the response you received.

请确保当您打开您在异步模式下打开。你不想通过同步http请求冻结你的页面。

Make sure when you open you open in async mode. You don't want to freeze your page by making synchronous http requests.

如果你需要它运行在旧版本的IE维基百科有一些好的信息: <一href="http://en.wikipedia.org/wiki/XMLHtt$p$pquest#Support_in_Internet_Explorer_versions_5.2C_5.5_and_6" rel="nofollow">http://en.wikipedia.org/wiki/XMLHtt$p$pquest#Support_in_Internet_Explorer_versions_5.2C_5.5_and_6

if you need it running on older versions of I.E. wikipedia has some good information: http://en.wikipedia.org/wiki/XMLHttpRequest#Support_in_Internet_Explorer_versions_5.2C_5.5_and_6

我假设你知道如何使用的document.getElementById element.innerHTML 来设置元素的含量

I assume you know how to use document.getElementById and element.innerHTML to set the content of your element.

修改 说干就干,增加了一个基本的实现:

EDIT Went ahead and added a basic implementation:

if (window.XMLHttpRequest) {// IE7+, Firefox, Webkit, Opera
  window.xmlhttp = new XMLHttpRequest();
} else {// IE6, 5
  window.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
     document.getElementById("someId").innerHTML = xmlhttp.responseText;
  }
}

xmlhttp.open("GET", "pageUrl", true);
xmlhttp.send();

这篇关于页面使用Ajax没有jQuery的更新部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆