简体   繁体   English

需要帮助,将jQuery对象变回字符串

[英]Need help turning a jQuery object back into a string

(i am new. please bear with me.) I have a jquery object that I need to convert back to html to use. (我是新手。请多多包涵。)我有一个jquery对象,需要将其转换回html才能使用。 What I am doing is using jQuery's get to get the HTML DOM of a local file. 我正在做的是使用jQuery的get获取本地文件的HTML DOM。 The data returned is then made into an object and I perform some tweaks on it (like changing hrefs etc.) 然后将返回的数据制成一个对象,然后对它进行一些调整(例如更改href等)。

$.get(imagePath + "bloghome.aspx", function (data) {
        var pageHtml = $(data);
        pageHtml.find('a').each(function () {
            var longHref = $(this).attr('href');
            var tmp = longHref.lastIndexOf('ID=');
            var id = longHref.substring(tmp + 3) + '.htm';
            var newHref = imagePath.concat(id);
            $(this).attr('href', newHref);
        });
    });

the object is created in the second line and then i change the hrefs. 在第二行中创建对象,然后更改hrefs。 What I need now is to turn that object back into a string so that I can write it to a file. 现在,我需要将该对象转换为字符串,以便可以将其写入文件。

I am using PhoneGap but any help would be appreciated as I am stumped 我正在使用PhoneGap,但是当我陷入困境时,我们将不胜感激

You can do this way using pageHtml[0].outerHTML : 您可以使用pageHtml[0].outerHTML这样做:

 $.get(imagePath + "bloghome.aspx", function (data) {
        var pageHtml = $(data);
        pageHtml.find('a').each(function () {
            var longHref = $(this).attr('href');
            var tmp = longHref.lastIndexOf('ID=');
            var id = longHref.substring(tmp + 3) + '.htm';
            var newHref = imagePath.concat(id);
            $(this).attr('href', newHref);
            var htmlString = pageHtml[0].outerHTML; //<-- Here
        });
    });

Can you just do 你能做

pageHtml.html();

?

EDIT: Using this will only give you the contents inside the main wrapping element, if you want the entire thing, you can use: 编辑:使用它将只给您主要包装元素内的内容,如果您想要整个东西,则可以使用:

pageHtml[0].outerHTML;

instead. 代替。

来自http://api.jquery.com/html/

console.log(pageHtml.html());

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

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