用ajax获取另一个页面的内容 [英] Get another page's content in a variable with ajax

查看:216
本文介绍了用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);
});

Live demo (click).

$.get() is a shorthand method for jQuery's $.ajax(). http://api.jquery.com/jquery.ajax/

这篇关于用ajax获取另一个页面的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆