简体   繁体   English

使用jQuery将HTML和CSS导出到文件

[英]Export html and css to a file using jquery

Imagine I have a 3 column layout. 想象一下,我有一个3列的布局。 Left, center and right div . 左,中和右div I want to export only the center div. 我只想导出中心div。 This is my export method so far: 到目前为止,这是我的导出方法:

function getHtml() {
    var htmlStartTag = function(){
        var attrs = $('html')[0].attributes;  
        var result = '<html';
        $.each(attrs, function() { 
            result += ' ' + this.name + '="' + this.value + '"';
        });                                               
        result += '>';
        return result;
    }    

return htmlStartTag() + $('html').html() + '</html>';
}

My question is, how can I save the DOM to a html file, say template.html? 我的问题是,如何将DOM保存到HTML文件(例如template.html)? When the user clicks export, the entire DOM should be saved or downloaded as html file. 当用户单击导出时,应将整个DOM保存或下载为html文件。 Is this possible, can you give an example? 这可能吗,可以举个例子吗? My application just uses jquery and html. 我的应用程序仅使用jquery和html。 It is not a serverside application! 它不是服务器端应用程序!

You can use data:URI , as seen in Using HTML5/Javascript to generate and save a file (2nd answer, the one with the 50+ upvotes) 您可以使用data:URI ,如使用HTML5 / Javascript所示以生成并保存文件 (第二个答案,即带有50个以上投票的答案)

something like http://jsfiddle.net/gBxrB/ should do the trick in your case. http://jsfiddle.net/gBxrB/这样的东西应该可以解决您的问题。

HTML HTML

<div class="left">leftleftleft</div>
<div class="center">center</div>
<div class="right">rightrightright</div>
<a href="#" class="export">export</a>​

JS JS

$('a.export').on('click',function(){
   uriContent = "data:application/octet-stream," + encodeURIComponent( $('.center').html() );
   window.open(uriContent, 'myDocument');
});

您无法使用javascript保存文件

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

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