javascript 使用 JQuery 将内容加载到外部 html 页面的 div 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13894218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load content into div of external html page with JQuery
提问by MaVe
Does anyone know how to load content into a div of external HTML page with jQuery?
有谁知道如何使用 jQuery 将内容加载到外部 HTML 页面的 div 中?
Using $('#divname').html("content")
only seems to be able to access div
elements of the HTML page where the script is in.
使用$('#divname').html("content")
似乎只能访问div
脚本所在的 HTML 页面的元素。
回答by DivZero
If I understand correctly you want to change the content of a DIV of an external page like for example an iframe?
如果我理解正确,您想更改外部页面的 DIV 内容,例如 iframe?
If so, this can't be done with simple jQuery due to security reasons
如果是这样,由于安全原因,这不能用简单的 jQuery 来完成
回答by charlietfl
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
您可以使用load()
加载所有内容,或将远程页面中的特定内容定位到本地页面中的元素中。load()
是一种 AJAX 方法。对 AJAX 做一点研究
$('#myDiv').load('remotePageUrl')
Or to only get part of the remote page
或者只获取远程页面的一部分
$('#myDiv').load('remotePageUrl #remotePageID')
API Reference: http://api.jquery.com/load/
API参考:http: //api.jquery.com/load/
回答by MaVe
Found it! This is what I needed:
找到了!这就是我需要的:
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!
感谢您的回复!
回答by Muhammad Talha Akbar
$.get("test.php", function(data){
$("div").html(data);
});