gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:34 25 4
gpt4 key购买 nike

我想创建一个可编辑的文本,您可以在其中填充 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com