可以JavaScript中访问了Ajax的text / html响应的DOM? [英] Can Javascript access the DOM of an Ajax text/html response?
问题描述
我想使用Ajax来获取一个HTML页面,然后拉出一个div通过它的ID,并插入DIV到当前页面。因此当前页面加载(通过Ajax)的第二页,翻出股利出Ajax响应,并插入到当前页面。不过,我很茫然的,除非响应是text / xml的,我不能在上面使用任何DOM功能......可以吗?
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的(或其他图书馆?)基本上会做这一切为您服务。我强烈建议寻找到这一点,并没有另起炉灶。
Jquery (or other libraries??) will basically do all this for you. I highly recommend looking into this and not reinventing the wheel.
有关查询,它可能想:
// 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);
}
});
我可能有语法错误。但这里是文档:<一href="http://docs.jquery.com/Ajax/jQuery.ajax#options">http://docs.jquery.com/Ajax/jQuery.ajax#options
这篇关于可以JavaScript中访问了Ajax的text / html响应的DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!