用ajax获取另一个页面的内容 [英] Get another page's content in a variable with ajax
问题描述
有没有办法将JavaScript变量设置为另一个HTML页面的内容?
我试过了:
var X = $(http:// www。网站/首页).html()
但它没有返回任何东西....即使是它解释了这个想法。 ..所以...任何人都可以告诉我该怎么做?此外,该网站中某个 id
或 class
的内容,如下所示:
var X = $(http://www.website.com/home#id)。html()
这真的对我有帮助,提前感谢!
重新寻找这个: $。get('otherPage.html')。then(function(responseData){
// responseData是另一个页面的内容,用你想做的任何事情做到。
$('#someElem')。append(responseData);
});
$。get()
是jQuery的 $。ajax()
的简写方法。 http://api.jquery.com/jquery.ajax/
Is there a way to set a javascript variable as the content of another HTML page?
I tried:
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:
var X=$(http://www.website.com/home "#id").html()
It would really help me, thanks in advance!
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/
这篇关于用ajax获取另一个页面的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!