javascript - 如何将 HTML 导出到没有边框或彩色文本的文档

标签 javascript html css download ms-word

我想创建一个可编辑的文本,您可以在其中填充 contentEditable 区域并将文本结果导出为文档,我在网上找到了一个脚本并对其进行了修改,但是我遇到的问题是 contentEditable 区域不可见喜欢固定文本,直到你点击它所以我为 contentEditable 区域制作了一个黑色边框和红色文本,以便用户可以看到它但是当我导出文档时它有黑色边框和红色文本所以我怎样才能在你填充时让它们可见导出文档时文本和消失 my website受此启发 site这是脚本 jsfiddle

function Export2Doc(element, filename = ''){
    var preHtml = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'><title>Export HTML To Doc</title></head><body>";
    var postHtml = "</body></html>";
    var html = preHtml+document.getElementById(element).innerHTML+postHtml;

    var blob = new Blob(['\ufeff', html], {
        type: 'application/msword'
    });
    
    // Specify link url
    var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(html);
    
    // Specify file name
    filename = filename?filename+'.doc':'document.doc';
    
    // Create download link element
    var downloadLink = document.createElement("a");

    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob ){
        navigator.msSaveOrOpenBlob(blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = url;
        
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
    
    document.body.removeChild(downloadLink);
}
span.a{

display:inline-block;

}
<body>
<div id="exportContent">
   Lorem Ipsum is simply dummy text of the printing and typesetting industry.
   Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
   <span  class="a" contenteditable="true" style="min-width:20px;border:1px solid black;color:red">
    when an unknown printer
    </span> took a galley of type and scrambled it to make a type specimen book. 
   It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. 
   It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
   and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>

<button onclick="Export2Doc('exportContent');">document</button>

</body>

最佳答案

从可编辑范围中删除内联样式 style="min-width:20px;border:1px solid black;color:red"并将其放在 span.a 下将显示设置为内联 block 的选择器。

function Export2Doc(element, filename = ''){
    var preHtml = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'><title>Export HTML To Doc</title></head><body>";
    var postHtml = "</body></html>";
    var html = preHtml+document.getElementById(element).innerHTML+postHtml;

    var blob = new Blob(['\ufeff', html], {
        type: 'application/msword'
    });
    
    // Specify link url
    var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(html);
    
    // Specify file name
    filename = filename?filename+'.doc':'document.doc';
    
    // Create download link element
    var downloadLink = document.createElement("a");

    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob ){
        navigator.msSaveOrOpenBlob(blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = url;
        
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
    
    document.body.removeChild(downloadLink);
}
span.a{
    display:inline-block;
    min-width:20px; 
    border:1px solid black;
    color:red;
}
<body>
<div id="exportContent">
   Lorem Ipsum is simply dummy text of the printing and typesetting industry.
   Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
   <span  class="a" contenteditable="true">
    when an unknown printer
    </span> took a galley of type and scrambled it to make a type specimen book. 
   It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. 
   It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
   and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>

<button onclick="Export2Doc('exportContent');">document</button>

</body>

关于javascript - 如何将 HTML 导出到没有边框或彩色文本的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377047/

相关文章:

javascript - 对 Node 顺序规则感到困惑(同步/异步)

javascript - ReactJS - 将返回对象从 State 传递到 Prop

javascript - 自定义函数中的链式中间件函数

angularjs - 使 "datetime-local"类型的 html5 输入字段与 Firefox 兼容

css - 什么时候使用 hr 元素在语义上是正确的?

html - 使用多个 header 标签是否被认为是 'bad practice'?

css - 多级垂直无序列表菜单

javascript - 如何将每次按键时的输入屏蔽为 html 输入字段中预定义字符串中的字符?

php - 如何在 1 个输入字段中捕获多个值(如 Facebook 消息)?

html - iframe - 如何从另一个网站复制实时摄像头提要