解析ajax HTML响应,并追加结果 [英] Parse ajax HTML response, and append result

查看:82
本文介绍了解析ajax HTML响应,并追加结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到条件,我需要用Ajax请求页面。并且我得到HTML作为结果。解析这些HTML结果,并将解析的HTML内容附加到当前页面。



假设这个HTML内容是要求的:

 < ; HTML> 
< head>< / head>
< body>

< div id =main>
< script type =text / javascriptsrc =http://script.com/js/script.js>< / script>
< / div>

< / body>
< / html>

如何解析 #main id selector包括JavaScript文本),而不是附加到当前HTML的一部分。

  jQuery.ajax({
url:requestedurl,
type:POST,
dataType:text,
success:function(data){
console.log(jQuery(data).find(#main ));
//比追加
}
});

最好不要使用jquery解析,而是正则表达式?非常感谢你的帮助。



ps:我尝试使用jquery加载结果,获取文本节点内容等。不幸的是,我不知道如何解析它。 :


i get condition where i need to request page with ajax. and i get HTML as result. parse those HTML result, and append parsed HTML content into current page.

let say this HTML content is the one requested :

<html>
<head></head>
<body>

<div id="main">
<script type="text/javascript" src="http://script.com/js/script.js"></script>
</div>

</body>
</html> 

How to parse #mainid selector (including javascript text) than append it on part of current HTML.

jQuery.ajax({
    url: requestedurl,
    type : "POST",
    dataType : "text",
    success: function(data) {
        console.log(jQuery(data).find("#main"));
        // than append
    }
});

is it better to not to parse using jquery, but regex? thank you very much for help.

ps : i try to load result using jquery, i get text node content, etc. unfortunately i don't know how to parse it. :(

解决方案

change your dataType from "text" to "html"

这篇关于解析ajax HTML响应,并追加结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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