如何使用jquery将html加载到变量中 [英] How do I load html into a variable with jquery

查看:88
本文介绍了如何使用jquery将html加载到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $(#my_div)。load( http://www.mypage.com); 

但我想要做的是将html加载到变量中:

  my_var = load(http://www.mypage.com); 

任何帮助都很棒。

我想循环一些项目,比如:

  HLS.functions.LoadSideModules = function(){
HLS.sideModuleContent = new Object(); (var i = 0; i< HLS.currentModuleConfig.POLICIES.POLICY.length; i ++){
for(var y = 0; y< HLS.currentModuleConfig.POLICIES.POLICY [i ] .PAGES.PAGE.length; y ++){
(HLS.currentModuleConfig.POLICIES.POLICY [i] .PAGES.PAGE [y]中的var POS){
var item = HLS.currentModuleConfig。 POLICIES.POLICY [I] .PAGES.PAGE [Y] [POS];
if(!HLS.sideModuleContent [item]){
HLS.sideModuleContent [item] = j.get(HLS.config.K2GETMODULE + HLS.currentModuleConfig.POLICIES.POLICY [i] .PAGES.PAGE [Y] [POS]);
}
}
}
}
};


解决方案

  $。 get(http://www.mypage.com,函数(my_var){
// my_var包含请求返回的
});

在jQuery下面将启动一个ajax请求,它会触发给定的URL。它还试图智能地猜测哪些数据将被接收(如果它是有效的html,则不需要指定)。如果您需要获取另一种数据类型,则只需将其作为最后一个参数传入即可,例如

  $。get(http:/ //www.mypage.com,函数(my_var){
// my_var包含请求返回的
,'html'); //或'text','xml','more'

参考:

I know I can load in html in to a div with:

$("#my_div").load("http://www.mypage.com");

but I want to do is load html into a variable like:

my_var = load("http://www.mypage.com");

Any help is great.

I would like to loop though some items like:

HLS.functions.LoadSideModules = function() {
    HLS.sideModuleContent = new Object();
    for(var i = 0; i < HLS.currentModuleConfig.POLICIES.POLICY.length; i++) {
        for(var y = 0; y < HLS.currentModuleConfig.POLICIES.POLICY[i].PAGES.PAGE.length; y++) {
            for(var POS in HLS.currentModuleConfig.POLICIES.POLICY[i].PAGES.PAGE[y]) {
                var item = HLS.currentModuleConfig.POLICIES.POLICY[i].PAGES.PAGE[y][POS];
                if(!HLS.sideModuleContent[item]) {
                    HLS.sideModuleContent[item] = j.get(HLS.config.K2GETMODULE + HLS.currentModuleConfig.POLICIES.POLICY[i].PAGES.PAGE[y][POS]);
                }
            }
        }
    }
};

解决方案

$.get("http://www.mypage.com", function( my_var ) {
    // my_var contains whatever that request returned
});

Underneath jQuery will launch an ajax request which fires to the given URL. It also tries to intelligently guess which data is going to be received (if it's valid html you don't need to specify). If you need to get another data type just pass that in as last argument, for instance

$.get("http://www.mypage.com", function( my_var ) {
    // my_var contains whatever that request returned
}, 'html');  // or 'text', 'xml', 'more'

Reference: .get()

这篇关于如何使用jquery将html加载到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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