Javascript 将HTML转换为有输入字段的Word文档
从HTML转换为文档的问题在于输入字段。是否可以仅从输入字段提取到文档值,而不直接从浏览器提取整个元素 HTML示例:Javascript 将HTML转换为有输入字段的Word文档,javascript,html,Javascript,Html,从HTML转换为文档的问题在于输入字段。是否可以仅从输入字段提取到文档值,而不直接从浏览器提取整个元素 HTML示例: <html> <head> <title>How to Export HTML to Word Document with JavaScript</title> </head> <body> <div class="source-html-outer"> &
<html>
<head>
<title>How to Export HTML to Word Document with JavaScript</title>
</head>
<body>
<div class="source-html-outer">
<div id="source-html">
<h1>
<center>Artificial Intelligence</center>
</h1>
<h2>Overview</h2>
<p>
Artificial Intelligence(AI) is an emerging technology
demonstrating machine intelligence. The sub studies like <u><i>Neural
Networks</i>, <i>Robatics</i> or <i>Machine Learning</i></u>
are the parts of AI. This technology is expected to be a
prime part of the real world in all levels.
</p>
<input type="text" value="123456" />
<input type="text" value="123456" style="margin-left: 150px;"/>
</div>
<div class="content-footer">
<button id="btn-export" onclick="exportHTML();">Export to word
doc</button>
</div>
</div>
如何使用JavaScript将HTML导出到Word文档
人工智能
概述
人工智能(AI)是一项新兴技术
演示机器智能。子研究类似于神经系统
网络、机器人或机器学习
是人工智能的组成部分。这项技术有望成为一项新技术
真实世界中各个层次的主要部分。
导出到word
医生
我正在使用的Javascript代码:
function exportHTML(){
var header = "<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 Word Document with JavaScript</title></head><body>";
var footer = "</body></html>";
var sourceHTML = header+document.getElementById("source-html").innerHTML+footer;
var source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML);
var fileDownload = document.createElement("a");
document.body.appendChild(fileDownload);
fileDownload.href = source;
fileDownload.download = 'document.doc';
fileDownload.click();
document.body.removeChild(fileDownload);
}
函数exportHTML(){
var header=“”+
“使用JavaScript将HTML导出到Word文档”;
var footer=“”;
var sourceHTML=header+document.getElementById(“源html”).innerHTML+footer;
var source='data:application/vnd.ms word;charset=utf-8',+encodeURIComponent(sourceHTML);
var fileDownload=document.createElement(“a”);
document.body.appendChild(文件下载);
fileDownload.href=source;
fileDownload.download='document.doc';
文件下载。单击();
document.body.removeChild(文件下载);
}
导出的文档看起来像这样,但我希望导出它时不带输入字段(只是值)
请检查示例应用程序的JSFIDLE链接:您可以将这些输入从呈现文档的元素中移出。为了打印这些输入的值,您可以创建不同的元素并将其放在那里:
函数exportHTML(){
//在呈现文档之前向文档添加输入值:
var inputs=document.querySelectorAll('input');
对于(变量i=0;i
人工智能
概述
人工智能(AI)是一项新兴技术
演示机器智能。子研究类似于神经系统
网络、机器人或机器学习
是人工智能的组成部分。这项技术有望成为一项新技术
真实世界中各个层次的主要部分。
导出到word
医生
> p>我会考虑使用正则表达式来查找和替换输入字段的原始值。您可以使用捕获组获取输入框的值,如下例所示
这样做的另一个好处是不会破坏页面
let re=/(\w+)\s(\w+/;
设str='John Smith';
让newstr=str.replace(re,$2,$1');
console.log(newstr);//Smith,John
这绝对是可能的-您可以创建一个自定义方法来分别提取标题、段落和值,而不是使用整个html文档。。