ajax 获取另外一个html页面并通过获取其中的部分来插入当前html方式有两种
1,通过<code></code>来包装获取
$.ajax({
url:url,
type:"GET",
dataType:"html",
success:function(result){
console.log(result);
var Obj = $("
").append($(result));//包装数据
//(需要获取的对应块(如class='aa')
var $html = $(".aa", Obj);
console.log($html.html());
//获取对应块中的内容
var value = $html.html();
//获得内容可以用append插入对应的div中,也可以用html(value)直接添加
}
});
2,通过正则表达式来实现
$.ajax({
url:url,
type:"GET",
dataType:"html",
success:function(result){
console.log(result);
//正则表达式获取body块
var reg = /[\s\S]*<\/body>/g;
var html = reg.exec(result)[0];
//然后用filter来筛选对应块对象,如:class='aa'
var aa = $(html).filter(".aa");
var aahtml = aa.html();
console.log(aahtml);
//获取内容后可以插入当前html中。。。
}
});
以上两种方式亲测过可用
3,可以通过jquery load()方法中添加标签来获取其中部分加载
//a1为当前页加载的DIV块,.b为加载html中class=b的块
$(".a1").load("userInfo.html .b");