Javascript可以访问Ajax文本

Javascript可以访问Ajax文本

本文介绍了Javascript可以访问Ajax文本/ html响应的DOM吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Ajax来获取一个HTML页面,然后通过它的ID拉出一个div,并将该DIV插入到当前页面中。因此,当前页面通过Ajax加载第二页,将div从Ajax响应中拉出并插入到当前页面中。但是我感到失落,除非这个回应是text / xml,我不能使用任何DOM函数...可以吗?

解决方案>

Jquery(或其他库)将基本上为您做所有这些。我强烈推荐研究这个,而不是重新发明。



对于查询,可能会如下:

  //调用url,数据
$ .ajax(
{url:$ url,
data:$ data,
dataType :'xml',
callback:function(returnData){
// Jquery find< mytag attribute =foo> ...< / mytag>并存储在mydata
var mydata = $(returnData).find('mytag [attribute = foo]');
//插入到当前页面到class =after-me的某个位置
$ -me')。html(mydata);
}
});

我可能有错误的语法。但这里是文档:


I'm trying to use Ajax to fetch an HTML page and then pull out a div by it's ID, and insert that DIV into the current page. So the current page loads (via Ajax) a second page, pulls the div out of the Ajax response and inserts into the current page. However I am at a loss as unless the response is text/xml, I cannot use any DOM functions on it... can I?

解决方案

Jquery (or other libraries??) will basically do all this for you. I highly recommend looking into this and not reinventing the wheel.

For query it would probably like:

// Make a call to url, with data
$.ajax(
  { url: $url,
    data: $data,
    dataType: 'xml',
    callback: function(returnData) {
      // Jquery find <mytag attribute="foo">...</mytag> and store it in mydata
      var mydata = $(returnData).find('mytag[attribute=foo]');
      // Insert into current page to somewhere with class="after-me"
      $('.after-me').html(mydata);
   }
});

I may have the syntax wrong. But here is the docs: http://docs.jquery.com/Ajax/jQuery.ajax#options

这篇关于Javascript可以访问Ajax文本/ html响应的DOM吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:24