简体   繁体   English

如何使用jQuery在变量中加载页面内容

[英]How to load a page content in variable with jQuery

A little problem here. 这里有个小问题。

I'm trying for 2 hours to load/get/steal my own content page in jQuery/JS variable (I'm trying to understand the logic behind the beast). 我试图用2个小时来加载/获取/窃取自己在jQuery / JS变量中的内容页面(我试图理解野兽背后的逻辑)。

Now we are, my problem: 现在是我的问题:

$(document).ready(function(){
    var string = $.get( "/main.html", function( data ){
                    return $(data).html();
                }, "html");
    alert($(string).html());
    document.title = 'Test ~ Main';
    return false;
});

I'm hardly trying to get the content of the main.html page to save it in a variable to permit me to fast load content with . 我几乎不会尝试获取main.html页面的内容以将其保存在变量中,以允许我使用快速加载内容。

My main.html page is a simple: 我的main.html页面很简单:

<html>
   <h1>Welcome on main page</h1>
   <p>Welcome again on main page</p>
</html>

Once i had a null alert and after some modifications i got an Object object. 一旦我有一个空警报,经过一些修改,我得到了一个Object对象。 I was google.load() 1.4.1 cause i'm reading a tut on jQuery and i switched to 1 to see if there wasn't an improvement. 我是google.load()1.4.1,因为我正在阅读jQuery上的tut,我切换到1以查看是否没有改进。 Before that i catched xmlhttprequest return value ... 在此之前,我捕获了xmlhttprequest返回值...

So more tests, changements, icecreams and the jQuery.get() doc/manual isn't helping me a lot. 因此,更多的测试,更改,冰淇淋和jQuery.get()文档/手册对我没有太大帮助。

Thank's to all incoming answers. 感谢所有收到的答案。

A french computer science student. 法国计算机科学专业的学生。

EDIT : There, it is my all script tags -> http://pastebin.com/pu43wrq2 Maybe the problem is inside. 编辑:那里是我所有的脚本标签-> http://pastebin.com/pu43wrq2也许问题出在里面。 And that's to dodge the load("/main.html") that i want to store main.html, work.html and contact.html (once and only once) in an array (for exemple) to fast page switch. 这就是为了避免load(“ / main.html”)我想将main.html,work.html和contact.html(一次且仅一次)存储在数组中(例如一次)以进行快速页面切换。

data is the output from another file you connected with js .. in your case data already html .. so try to use 数据是您与js连接的另一个文件的输出。在这种情况下,数据已经是html了,因此请尝试使用

$(document).ready(function(){
    var string;
    $.get( "/main.html", function( data ){ // check your main.html path
                    alert(data); // check data 
                    string = data; // make string from the data
                }, "html");
    alert(string); // alert string
    document.title = 'Test ~ Main';
    return false; // no need to return false
});

with this code you should get 2 alerts .. its same content but 1st one from data and 2nd from string 使用此代码,您应该获得2个警报..其内容相同,但第一个警报来自数据,第二个警报来自字符串

if you got empty alert then check your main.html path 如果收到空警报,请检查main.html路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM