javascript 使用ajax在变量中获取另一个页面的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20927573/
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
Get another page's content in a variable with ajax
提问by towc
Is there a way to set a javascript variable as the content of another HTML page?
有没有办法将 javascript 变量设置为另一个 HTML 页面的内容?
I tried:
我试过:
var X = $(http://www.website.com/home).html()
var X = $(http://www.website.com/home).html()
but it didn't return anything.... Even tho it explains the idea... so... can anyone tell me how to do so please? Also the content of a certain id
or class
in that website, something like:
但它没有返回任何东西......即使它解释了这个想法......所以......有人能告诉我怎么做吗?还有某个网站id
或class
该网站中的内容,例如:
var X=$(http://www.website.com/home "#id").html()
var X=$(http://www.website.com/home "#id").html()
It would really help me, thanks in advance!
这真的对我有帮助,在此先感谢!
回答by m59
It sounds like you're looking for this:
听起来你正在寻找这个:
$.get('otherPage.html').then(function(responseData) {
//responseData is the contents of the other page. Do whatever you want with it.
$('#someElem').append(responseData);
});
$.get()
is a shorthand method for jQuery's $.ajax()
. http://api.jquery.com/jquery.ajax/
$.get()
是 jQuery 的$.ajax()
. http://api.jquery.com/jquery.ajax/