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

查看:62
本文介绍了使用ajax在变量中获取另一个页面的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将 javascript 变量设置为另一个 HTML 页面的内容?

Is there a way to set a javascript variable as the content of another HTML page?

我试过了:

var X = $(http://www.website.com/home).html()

但它没有返回任何东西......即使它解释了这个想法......所以......有人能告诉我怎么做吗?还有该网站中某个 idclass 的内容,例如:

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() 是 jQuery 的 $.ajax() 的简写方法.http://api.jquery.com/jquery.ajax/

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

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

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