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

javascript - How to get the html of a div from a different page with AJAX? - Stack Overflow

programmeradmin1浏览0评论

How can I get the html of a certain html element which is located on a different site?

Solution:

$.ajax({
url: 'somefile.html',
success: function(data) {
    data=$(data).find('div#id');
    $('#mydiv').html(data);
    alert('Done.');
 }
});

How can I get the html of a certain html element which is located on a different site?

Solution:

$.ajax({
url: 'somefile.html',
success: function(data) {
    data=$(data).find('div#id');
    $('#mydiv').html(data);
    alert('Done.');
 }
});
Share Improve this question edited Jan 5, 2013 at 0:48 Mike Burnwood asked Jun 13, 2012 at 20:47 Mike BurnwoodMike Burnwood 2791 gold badge2 silver badges15 bronze badges 4
  • By "another page", do you mean one of your own pages? Or a page from a completely different website? – kevin628 Commented Jun 13, 2012 at 20:48
  • very unlikely...unless it's the same webapp/site this might be impossible for obvious security reasons – Nadir Muzaffar Commented Jun 13, 2012 at 20:49
  • 1 What have you tried? – sczizzo Commented Jun 13, 2012 at 20:49
  • by 'another page' I mean a page on the same domain, but with another path. – Mike Burnwood Commented Jun 13, 2012 at 20:50
Add a comment  | 

5 Answers 5

Reset to default 15

You can use $.load with an appended container

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted.

$('#result').load('ajax/test.html #container');

Make a ajax call to a php or any other file , use CURL or other tools to grab the page you want and extract the div and echo it and then when you get back the html just put it inside a div in your page

    $.ajax({
    url: 'somefile.html',
    success: function(data) {
                    data=$(data).find('div#id');
        $('#mydiv').html(data);
        alert('Done.');
     }
    });

Here you go:

$('#div_id_in_your_page').load('ajax_page.html #required_div');

For class:

$('.div_class_in_your_page').load('ajax_page.html #required_div');

One way is:

  • send an ajax call to a server side script

  • this script fetches the remote page and returns HTML as a response. (generally JSON is preferred)

  • your page finally gets access to the html.

you can also use like this.

$.ajax({
   url:"page2.html",
   success:function(response){
      $("#currentDIV").html(response);
   },error:function(){
      alert("error");
   }
});
发布评论

评论列表(0)

  1. 暂无评论