jquery加载()的Javascript替代方案

jquery加载()的Javascript替代方案,javascript,Javascript,有人能给我举一个纯javascript替代jquery load()的例子吗? 或者让我看看rite网站的例子 多谢各位 更新: 这不是我想问的。 我需要使用Ajax加载URL并将返回的HTML插入“div” 我不太明白负面的观点。至少有人愿意解释我的问题出了什么问题?我到处搜索,找不到示例。为了使用纯JavaScript发出AJAX请求,您需要执行以下操作: httpRequest = new XMLHttpRequest(); // Specify a function to handle

有人能给我举一个纯javascript替代jquery load()的例子吗? 或者让我看看rite网站的例子

多谢各位

更新:

这不是我想问的。 我需要使用Ajax加载URL并将返回的HTML插入“div”


我不太明白负面的观点。至少有人愿意解释我的问题出了什么问题?我到处搜索,找不到示例。

为了使用纯JavaScript发出AJAX请求,您需要执行以下操作:

httpRequest = new XMLHttpRequest();

// Specify a function to handle the response.

httpRequest.onreadystatechange = function() {
    // Process the AJAX response in here.
}

// Make the AJAX request
httpRequest.open('GET', 'http://prajjwal.com/profile.json', true);
function handleRequest() {
  if (httpRequest.readyState === 4) {
    if (httpRequest.status === 200) {
      yourdiv.innerHTML = httpRequest.responseText;
    } else {
      console.log('There was a problem with the request.');
    }
  }
}

httpRequest.onreadystatechange = handleRequest;
要打开的第一个参数是要发出的请求类型。在这种情况下,我们会发出“获取”请求。第二个参数是您试图检索的项目的url。第三个参数是一个布尔值,它决定请求是否应该是异步的。如果请求是异步的,函数将不会停止&等待请求完成

还值得注意的是,这在InternetExplorer8或更低版本中不起作用。要支持这些浏览器,您需要使用:

httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
要处理响应,处理程序需要具有如下内容:

httpRequest = new XMLHttpRequest();

// Specify a function to handle the response.

httpRequest.onreadystatechange = function() {
    // Process the AJAX response in here.
}

// Make the AJAX request
httpRequest.open('GET', 'http://prajjwal.com/profile.json', true);
function handleRequest() {
  if (httpRequest.readyState === 4) {
    if (httpRequest.status === 200) {
      yourdiv.innerHTML = httpRequest.responseText;
    } else {
      console.log('There was a problem with the request.');
    }
  }
}

httpRequest.onreadystatechange = handleRequest;
httpRequest.readyState告诉您请求的进展情况。值4表示请求已完成并准备好处理


请记住,您不能使用open()请求其他域上的资源。

这不是我要问的,很抱歉。jQuery中有两个
load
函数,我认为我想到的是一个更常用的函数。@Felix Kling没问题,我修改了我的任务以澄清这一点。这不是我要的。谢谢