最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get a part of HTML webpage in a jQuery ajax Request? In other words how to filter the web page contents on a

programmeradmin3浏览0评论

I am using the below script to load the html in my page. However it loads all of the page contents that is included in webpage.html means(from doctype to closing html tag).

$(function() {
            $.ajax({
                type:"GET",
                url:"webpage.html",
                dataType:"html",
                success:function(data) {
                    alert(data);
                    $("body").html(data);
                },
                error:function() {
                    alert("Error");
                }
            })
        });

I have no control over webpage.html, I actually just need a few divs with particular classes to load after an ajax Request. Can anyone give me some Idea as to how would I filter the webpage contents over an ajax Call for which I have no control.

Thanks, Mike

I am using the below script to load the html in my page. However it loads all of the page contents that is included in webpage.html means(from doctype to closing html tag).

$(function() {
            $.ajax({
                type:"GET",
                url:"webpage.html",
                dataType:"html",
                success:function(data) {
                    alert(data);
                    $("body").html(data);
                },
                error:function() {
                    alert("Error");
                }
            })
        });

I have no control over webpage.html, I actually just need a few divs with particular classes to load after an ajax Request. Can anyone give me some Idea as to how would I filter the webpage contents over an ajax Call for which I have no control.

Thanks, Mike

Share Improve this question asked Sep 11, 2013 at 19:27 MikeMike 3,41811 gold badges47 silver badges80 bronze badges 1
  • Possible duplicate of Extract part of HTML document in jQuery – Simon E. Commented Jul 3, 2017 at 5:42
Add a ment  | 

2 Answers 2

Reset to default 3
 success:function(data) {
     var out = "";
     $(data).find("your selectors").each(function(loop, item){
         out += $(item).html();
     });
     data = out;
     alert(data);
     $("body").html(data);
 }

Use .load()

var url = "webpage.html .classOfDivsYouWantLoaded";

$("body").load(url, function(){
    //callback after your data is in loaded into body.
});
发布评论

评论列表(0)

  1. 暂无评论