admin管理员组

文章数量:1326513

Does anyone know how to load content into a div of external HTML page with jQuery?

Using $('#divname').html("content") only seems to be able to access div elements of the HTML page where the script is in.

Does anyone know how to load content into a div of external HTML page with jQuery?

Using $('#divname').html("content") only seems to be able to access div elements of the HTML page where the script is in.

Share Improve this question edited Mar 24, 2015 at 9:00 Bud Damyanov 31.9k6 gold badges47 silver badges53 bronze badges asked Dec 15, 2012 at 16:57 MaVeMaVe 1,7255 gold badges21 silver badges31 bronze badges 4
  • What does "external" means here, another page from same domain or from any other domain? – Vipul Commented Dec 15, 2012 at 17:18
  • A page from the same domain. – MaVe Commented Dec 15, 2012 at 19:35
  • Please have a look at my answer. And if my assumption is incorrect, please add more code to explain what you want to do – DivZero Commented Dec 15, 2012 at 20:13
  • @DivZero Well, my pages are in different folders, but I guess this is till the same domain? Or do I need to set these domains explicitly? – MaVe Commented Dec 16, 2012 at 8:22
Add a ment  | 

4 Answers 4

Reset to default 3

If I understand correctly you want to change the content of a DIV of an external page like for example an iframe?

If so, this can't be done with simple jQuery due to security reasons

You can use load() to load all content , or target specific content from remote page into an element in local page. load() is an AJAX method. Do a little research on AJAX

$('#myDiv').load('remotePageUrl')

Or to only get part of the remote page

$('#myDiv').load('remotePageUrl #remotePageID')

API Reference: http://api.jquery./load/

Found it! This is what I needed:

access div in iframe parent

I thought I didn't matter that it was a child-parent relationship as long they were in the same domain, but apparently it does.

Thanks for the responses!

$.get("test.php", function(data){
$("div").html(data);
});

本文标签: javascriptLoad content into div of external html page with JQueryStack Overflow