简体   繁体   中英

how to get content of div in a html from another html

I have two html file

a.html
<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>  
  </head>
  <body> 
    <div id="content">
        hello every one
    </div>
  </body>
</html>

and another page

<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>  
  </head>
  <body> 
    <div id="result">

    </div>
    <iframe id="ifr" src="http://example.com/a.html">
    </iframe>
    <script type="text/javascript">
        divv = $('#ifr').contents().find('div#content').clone();
        $('#result').html(divv.html());
    </script>
  </body>
</html>

In second one I try to get first html and get contet div in it.after that I put this value to result div .
but It's not work. How can I do that.

You do not need to use an iframe; you can use ajax to do that. It's very straight forward.

$(function() {
    $('#result').load('a.html #content',function()
        $(this).html( $('#content').html() );
    });
});

EDIT

As evident from comments below, scope of question has changed. The two pages are on different domains without CORS . Therefore the above answer would not work.

In order to use ajax , you may want to create a server-side script to act as a proxy. Then you'll call that script on your server and it will fetch the a.html page for you.

I guess that could be the right way.

var ifr = document.querySelector('#ifr');
var html = ifr.contentDocument.querySelector('div#content').innerHTML;
$('#result').html(html);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM