Javascript 正在尝试使用array.push();使用换行符,但使用纯文本,而不是html<;br>;标签
最近我一直在尝试获取HTML表单输入数据,添加前缀,然后将其写回Javascript 正在尝试使用array.push();使用换行符,但使用纯文本,而不是html<;br>;标签,javascript,jquery,html,Javascript,Jquery,Html,最近我一直在尝试获取HTML表单输入数据,添加前缀,然后将其写回,例如: HTML: <h1>Please enter Details</h1><hr> GUID <a href="https://www.guidgenerator.com/online-guid-generator.aspx">(Generator)</a>:<div id="guidInput" style="display:inline;">
,例如:
HTML:
<h1>Please enter Details</h1><hr>
GUID <a href="https://www.guidgenerator.com/online-guid-generator.aspx">(Generator)</a>:<div id="guidInput" style="display:inline;">
<!-- Changed to an onkeyup="" method. Works the same but with less code. -->
<input onkeyup="gen()" id="guidText" style="height: 16px;"></input>
</div>
ID:<div id="idInput" style="display:inline;">
<!-- Changed to an onkeyup="" method. Works the same but with less code. -->
<input type="number" type="number" onkeyup="gen()" id="idText" style="height: 16px;"></input>
</div>
<div id="command" class="command"></div>
$(document).ready(function(){
var command = ""; /*Here for future developement*/
command += ""; /*Here for future developement*/
document.getElementById('command').innerHTML = command;
});
function gen() {
var id = $('#idText').val();
var guid = $('#guidText').val();
var command = ""; /*Here for future developement*/
var tags = [];
tags.push("GUID "+guid);
tags.push("ID "+id);
command += tags.join("<br>");
command += ""; /*Here for future developement*/
document.getElementById('command').innerHTML = command;
}
<button onclick="saver()">Save</button>
function saver() {
var text = document.getElementById("command").innerHTML;
var newText = text.replace(/(<([^>]+)>)/ig,"");
var filename = ("File")
var blob = new Blob([text], {type: "text/plain;charset=utf-8"});
saveAs(blob, filename+".txt");
}
这就是我的问题所在。我需要该文件如下所示:
GUID qwertyuiop
ID 12345
每个部分后都有一个换行符。
用于在站点上显示它,但我需要某种方法来确保它位于下载文件中的单独一行,并且文件中没有HTML标记。您的代码违反了SRP:单一责任原则
你试图同时做两件事
HTML中的前缀和格式是两个不同的问题,它们应该分开
在那之后,答案将变得显而易见。var newText=text.replace(`
`,`\n`)。replace(/(]+)>)/ig,“”;
或
函数生成器(分隔符){
// ... //
command+=tags.join(分隔符);
返回命令;
}
函数保存程序(){
// ... //
var newText=gen(`\n`);
// ... //
}
<代码> >您正在寻找<代码> \r\n\/COD>。@ AndreiGheorghiu,我是否使用它来加入GUID和ID字段,而不是<代码> BR> < /代码>?考虑添加一个实际的答案(或者至少链接到有关如何使用建议的有用资源)到您的(其他好的)建议中。以目前的形式,它更适合作为评论,而不是答案。请参阅了解详细信息。我同意Andrei的观点,您的回答没有为我的查询提供任何信息。
GUID qwertyuiop
ID 12345